1024programmer Asp.Net ASP.NET Core MVC Routing from Entry to Mastery

ASP.NET Core MVC Routing from Entry to Mastery

ASP.NET Core MVC Routing from Entry to Master

With the development of technology, ASP.NET Core MVC has also been launched for a long time. After continuous version update iterations, it has become more and more perfect. This series of articles mainly explains the process involved in the development of B/S system by ASP.NET Core MVC The related content is suitable for beginners, school graduates, or other people who want to engage in ASP.NET Core MVC system development. After the explanations of the previous articles, I have a preliminary understanding of ASP.NET Core MVC project creation, startup and operation, and ASP.NET Core MVC naming conventions, creating controllers, views, models, receiving parameters, passing data, etc., continue to explain today ASP.NET Core MVC routing and other related content are only for learning and sharing.

With the development of technology, ASP.NET Core MVC has also been launched for a long time. After continuous version update iterations, it has become more and more perfect. This series of articles mainly explains the process of developing B/S system with ASP.NET Core MVC The relevant content involved in is suitable for beginners, school graduates, or other people who want to engage in ASP.NET Core MVC system development. After the explanations of the previous articles, I have a preliminary understanding of ASP.NET Core MVC project creation, startup and operation, and ASP.NET Core MVC naming conventions, creating controllers, views, models, receiving parameters, passing data, etc., continue to explain today ASP.NET Core MVC routing and other related content are only for learning and sharing.

what Is it routing?

Routing is a mechanism, mainly used to check each user request and map the user request to Action, which is implemented through routing middleware. ASP.NET Core MVC uses routing middleware to match URLs of incoming requests and map them to actions (Action methods).

default route

In creating ASP.NET Core MVC through templates, routing middleware is added by default, and a default routing mapping rule and constraint is provided.

MapControllerRoute is used to create a single route. A single route is named the default route. Most apps with controllers and views use a route template like the default route. As follows:

 1 using Microsoft.AspNetCore.Server.Kestrel.Core;
  2 using  span> System.Text.Encodings.Web;
  3 using System.Text.Unicode;
  4
  5 var builder =   WebApplication. CreateBuilder(args);
  6
  7 // Add services to the container.
  8 builder.Services.AddControllersWithViews().AddJsonOptions(options =>
  9 {
 10 options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
 11 });
 12
 13 builder. Services. Configure(options =>
 14 {
 15 options.AllowSynchronousIO = true;
 16 });
 17
 18 var app =   builder. Build();
 19
 <span stRoute parameters for the route.  Doing so can result in inconsistent and confusing behavior with URL generation.  A sample error is as follows:

1 public class TestController : Controller
 2 {
 3 [Route(  "/articles/{page}")]
 4 public IActionResult ListArticles(int page)
 5  {
 6 return View(page);
 7  }
 8 }

Http predicates and routing templates

In the ASP.NET Core MVC project, there are the following predicates to distinguish different request methods:

  • [HttpGet]
  • [HttpPost]
  • [HttpPut]
  • [HttpDelete]
  • [HttpHead]
  • [HttpPatch]

ASP.NET Core has the following route templates:

  • All HTTP verb templates are route templates.
  • [Route]

Hybrid routing: attribute routing and traditional routing

ASP.NET Core applications can mix traditional routing and attribute routing. Typically, traditional routing is used for controllers serving HTML pages to browsers, and attribute routing is used for controllers serving REST to APIs.

Actions support both traditional routing and attribute routing. Attribute routing is achieved by placing routes on controllers or actions. Actions defining attribute routes are not accessible via traditional routing, and vice versa. Any routing attribute on the controller will cause all actions in the controller to use attribute routing.

Attribute routing and traditional routing use the same routing engine.

The above is the whole content of ASP.NET Core MVC routing from entry to mastery, aiming to attract jade, learn together and make progress together.

Author: Little Six

Source: http://www.cnblogs.com/hsiang/

The copyright of this article belongs to the author and the blog garden. It is not easy to write an article, and originality is supported. Welcome to reprint [Like], please keep this statement for reprinting, and give the original text link in an obvious place on the article page, thank you.

Focus on the personal official account, regularly update technology and workplace articles

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/asp-net-core-mvc-routing-from-entry-to-mastery/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索