asp. net Core 3.0 Zone and Route Configuration Method

  • 2021-11-02 00:40:10
  • OfStack

In ASP. NET Core 3.0, the routing configuration is different from 2.0

1. MVC service registration

ASP. NET Core 3.0 adds a new option Startup. ConfigureServices for registering internal MVC scenarios.

Three new top-level extension methods are available with MVC scheme on IServiceCollection. Templates use these new methods instead of UseMvc. However, AddMvc continues like it has been in previous versions.

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


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

The following example adds support for controllers, API-related features, 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 minimum 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

1 Not recommended:

Add UseRouting.

If the application calls UseStaticFiles, UseRouting will precede UseStaticFiles.

If the application uses authentication/authorization functions, such as AuthorizePage or [Authorize], UseAuthentication will be combined with UseAuthorization after UseRouting.

If the application uses CORS functionality, such as [EnableCors], the next step of UseCors will be placed.

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 occurs within the current 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.

Will be the following:

MapRoute uses MapControllerRoute

MapAreaRoute uses MapAreaControllerRoute

Since routing now includes support for more than just MVC, the terminology has been changed to make clear what they do with these methods. Such as traditional routing MapControllerRoute/MapAreaControllerRoute/MapDefaultControllerRoute they want to be added to the sequential application. Route bit 1 more specifically (such as the route of a 1 zone).

In the following example:

MapControllers adds controller support for attribute routing. MapAreaControllerRoute adds the traditional routing of the controller to the zone. MapControllerRoute adds a regular route to the controller.

UseEndpoints now occurs within the map Razor page.

Add MapRazorPages if the application uses Razor page. Adding the Razor page as endpoint routing includes support for many frameworks now participates.

Updated Startup. Configure code in asp. netCore3.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 you want to do partition routing, you need to add Area and Route labels to the controller Controller header, otherwise you cannot automatically route the controller and Action as 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 1 must be added [Area("CMS")]、[Route("CMS/[controller]/[action]")]

Summarize


Related articles: