Asp.net Core 3.0 Area and Routing Configuration Method

Time:2019-7-20

Routing configuration is different from 2.0 in ASP.NET Core 3.0

MVC Service Registration

ASP.NET Core 3.0 adds a new option, Startup.ConfigureServices, for registering an internal MVC solution.

Three new top-level extensions are available with IServiceCollection on MVC schemes. Templates use these new methods, not UseMvc. However, AddMvc continues to look like it has been in previous versions.

The following example adds support for controllers and API-related functions, but not views or pages. The API template uses this code:


public void ConfigureServices(IServiceCollection services)
{
 services.AddControllers();
}

The following example will add support for controllers, API-related functions, and views, but not pages. The Web Application (MVC) template uses this code:


public void ConfigureServices(IServiceCollection services)
{
 services.AddControllersWithViews();
}

The following example adds support for Razor pages and minimal controller support. The Web application template uses this code:


public void ConfigureServices(IServiceCollection services)
{
 services.AddRazorPages();
}

In addition, new methods can be combined. The following example is equivalent to calling AddMvcASP.NET Core 2.2:


public void ConfigureServices(IServiceCollection services)
{
 services.AddControllers();
 services.AddRazorPages();
}

2. Startup.Configure configuration

Generally not recommended:

Add UseRouting.

If the application calls UseStaticFiles, it will be placed before UseRouting.

If the application uses authentication/authorization functions, such as Authorization Page or [Authorization], it will use Routing after UseAuthentication and UseAuthorization.

If the application uses CORS functionality, such as [EnableCors], it will place UseCors next.

Replace UseMvc or UseSignalR with UseEndpoints.

The following is a typical ASP.NET Core 2.2 application of Startup.Configure:


public void Configure(IApplicationBuilder app)
{
 ...

 app.UseStaticFiles();
 
 app.UseAuthentication();

 app.UseSignalR(hubs =>
 {
  hubs.MapHub<ChatHub>("/chat");
 });

 app.UseMvc(routes =>
 {
  routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
 });
}

UseEndpoints now occur within the controller map.

Add MapControllers if the application uses attribute routing. Since routing includes support for many frameworks in ASP.NET Core 3.0 or later, the controller that adds attribute routing is participating.

The following will be included:

MapRoute uses MapController Route

MapAreaRoute uses MapAreaController Route

Since routing now includes support for more than MVC, terminology has been changed to specify the methods they do. For example, the traditional routing MapController Route / MapArea Controller Route / MapDefault Controller Route will add sequential applications. The first and more specific route (e.g. a region route) will be used.

In the following example:

  • MapControllers add controller support for attribute routing.
  • MapArea Controller Route adds the traditional routing area of the controller.
  • MapController Route adds conventional routing for controllers.

UseEndpoints now occur in the mapping Razor page.

Add MapRazor Pages if the application uses Razor pages. Because endpoint routing includes adding Razor pages to support many frameworks, now participate.

Updated Startup.Configure code in asp.netCore 3.0:


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
   if (env.IsDevelopment())
   {
    app.UseDeveloperExceptionPage();
   }
   else
   {
    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.UseCookiePolicy();

   app.UseRouting();

   app.UseAuthorization();

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

    endpoints.MapAreaControllerRoute(
     name: "areas", "areas",
     pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
   });
  }

If partition routing is to be done, you need to add Area and Route labels in the controller Controller header, otherwise you can’t automatically route the controller and Actions in asp. netCore 2.0. The sample code is as follows:


namespace WebApplication1.Areas.CMS.Controllers
{
 [Area("CMS")]
 [Route("CMS/[controller]/[action]")]
 public class NewsController : Controller
 {
  public IActionResult Index()
  {
   return View();
  }
  public IActionResult List()
  {
   return View();
  }
 }
}

These two sentences must be added.[Area("CMS")]、[Route("CMS/[controller]/[action]")]

summary

Above is the whole content of this article. I hope the content of this article has some reference value for your study or work. Thank you for your support to developpaer.