atif khan
atif khan

Reputation: 51

Asp.net core application getting error : This localhost page can’t be found

getting this error : This localhost page can’t be found

I am working on ASP.Net core application and were working fine but after adding Scaffolded for Identity I am getting this error ' This localhost page can’t be found' . I noticed scaffolding Identity added the below configuration in program.cs and if I comment this out it then it works fine.

builder.Services.AddDefaultIdentity<IdentityUser>() .AddEntityFrameworkStores<ApplicationDbContext>();

Below is full program.cs code

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options => 
options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")
));
builder.Services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>(); 

builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this 
for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

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

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

app.Run();

Upvotes: 1

Views: 2662

Answers (2)

Premchandra Singh
Premchandra Singh

Reputation: 14272

Starting .net core 6.0 you need to add following package explicitly Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation into your project.

Change your Program.cs to include AddRazorRuntimeCompilation() like below

  • For RazorPages project

builder.Services.AddRazorPages().AddRazorRuntimeCompilation();

  • For MVC project

builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();

Above changes will allow you change .cshtml content even after deployment. That being said, it also mean that you have to deploy plain .cshtml files into the server, otherwise 404 will be there.

Copy all .cshtml files for publishing

Make following changes to your .csproj file

    <ItemGroup>
        <Content Update="Pages\**\*.cshtml">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
    </ItemGroup>

Note: This will copy the into a folder Pages just above the publish folder while deploying.

Upvotes: 1

Rahimullah Niazi
Rahimullah Niazi

Reputation: 21

I have same issue, fixed with adding area attribute on controller:

[Area("Changer")]
public class HomeController : Controller
{
             
}

Upvotes: 1

Related Questions