I have an ASP.NET Core application with Razor pages and I want take advantages of MVC into it.
I added
services.AddMvc()
to startupI did run the project and tested if
But it did not work and returned
"This localhost page can’t be found".
Would you please help me how I config my app?
If you want to add MVC to a Razor Page project:
First you need to add services.AddControllers();
to ConfigureServices
and configure mvc routing in startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddControllers();
...
}
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Second you need to add two folders with these names
Controllers
Views
.Then copy
Pages/Shared
folder which contains _Layout.cshtml
_ValidationScriptsPartial.cshtml
to Views
folder.
And copy
Pages/_ViewImports.cshtml
to Views/_ViewImports.cshtml
,Pages/_ViewStarts.cshtml
to Views/_ViewStarts.cshtml
.Here is a project structure: