首页 > 解决方案 > MVC and areas of razor pages in same project .net core

问题描述

I have an area in MVC project. In the area section I have razor pages project in .NET Core. How can I map both routes? When I visit https://localhost:44361/ then main project should be run and if I visit https://localhost:44361/backend then the area of razor pages should be run.

标签: asp.net-mvcasp.net-corerazor-pages

解决方案


Because Razor pages is using folder-based routing as default, you may create folder Pages/Backend, and use a standard setup as shown below.

The same solution using areas: Create folder Areas/Backend/Pages and use the setup shown below.

In Startup#ConfigureServices

services.AddControllers();
services.AddRazorPages();

In Startup#Configure

app.UseEndpoints(endpoints =>
{
   endpoints.MapRazorPages();
   endpoints.MapControllers();
});

Both approaches above are verified by creating a MVC-app and added support for Razor pages by adding services.AddRazorPages(); and endpoints.MapRazorPages();.

More information about areas: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-5.0#areas-with-razor-pages


推荐阅读