Search code examples
c#.netasp.net-mvc.net-6.0

InvalidOperationException: The layout view '/views/shared/_Layout.cshtml' could not be located


I refactored my project from .net core 3.0 to .net6 when I converted it the project did not work on HTTPS and working on HTTP then I created a new project and got all the files when I ran my app I got these issues and I could not determine what problem this first issue

InvalidOperationException: The layout view '/views/shared/_Layout.cshtml' could not be located.

second issue

InvalidOperationException: The default Identity UI layout requires a partial view '_LoginPartial' usually located at '/Pages/_LoginPartial' or at '/Views/Shared/_LoginPartial' to work. Based on your configuration we have looked at it in the following locations:

/Areas/Identity/Pages/Account/_LoginPartial.cshtml
    /Areas/Identity/Pages/_LoginPartial.cshtml
    /Areas/Identity/Pages/Shared/_LoginPartial.cshtml
    /Areas/Identity/Views/Shared/_LoginPartial.cshtml
    /Pages/Shared/_LoginPartial.cshtml
    /Views/Shared/_LoginPartial.cshtml.

my project configuration

    var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var services = builder.Services;
services.AddDbContext<GasdwahedDbContext>(options =>
{
    options.UseSqlServer(
        builder.Configuration.GetConnectionString("DefaultDbConnection"));
    options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});


services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireDigit = false;
    options.Password.RequireLowercase = false;
    options.Password.RequireUppercase = false;
    options.Password.RequiredLength = 6;
})
    .AddEntityFrameworkStores<GasdwahedDbContext>()
    .AddDefaultUI()
    .AddDefaultTokenProviders();
services.AddControllersWithViews();
services.AddMvc(options => options.EnableEndpointRouting = false);
services.AddRazorPages();
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>
    , ApplicationUserClaimsPrincipalFactory>();
services.AddSignalR();

//services.Configure<ForwardedHeadersOptions>(options =>
//{
//    options.KnownProxies.Add(IPAddress.Parse("192.168.1.220"));
//});

services.Configure<TwilioVerifySettings>(builder.Configuration.GetSection("Twilio"));

//JWT Generate Token

var appSettingsSection = builder.Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);

var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthorization(options =>
{

    options.AddPolicy("Admin",
        authBuilder =>
        {
            authBuilder.RequireRole("Administrators");
        });

});
services.AddAuthentication(x =>
{
    x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{

    x.RequireHttpsMetadata = false;
    x.SaveToken = true;
    x.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey(key),
        ValidateIssuer = false,
        ValidateAudience = false
    };
});

services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(5);
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.MapControllerRoute(
    name: "MyArea",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

enter image description here


Solution

  • Simply the provocative solution is :

    1- right click on file which has problem.

    2- choose properities.

    enter image description here

    3- change Build action to Content instead of none or anything else.

    enter image description here