ASP.NET Core Routing property
After learning of the previous section, presumably you've got to ASP.NET Core MVC routing rough idea, I wanted to spend a few chapters to explain routing rules, but as a basic tutorial, we explain the point of it next knowledge .
In this chapter, we will learn another routing method, which is based on routing attributes.
Attribute routing
By attribute-based routing, we can use on the interior of these classes and methods of the controller class C#
attributes. These properties carry metadata when told to call a specific ASP.NET Core Controller
- Properties alternative routing is based on the agreed route
- Routing in the order they appear in the assessment, that is, we registered their order, the situation is quite common to map multiple routes, especially if we want to use different parameters in the URL, or if you want to use different text in the URL
We give a simple example.
Open and run the HelloWorld project and then access the application in the browser. When we visited / about
, it produces the following output
We want, when we visited /about
, the application should call AboutController
the Phone
method
In view of this situation, we can use Microsoft.AspNetCore.Mvc
a namespace Route
attribute for the controller to enforce some of the explicit route
The following code is added to the attribute route AboutController
achieved
using System;
using Microsoft.AspNetCore.Mvc; namespace HelloWorld.Controllers { [Route("about")] public class AboutController { public AboutController() { } [Route("")] public string Phone() { return "+10086"; } [Route("country")] public string Country() { return "中国"; } } }
Here, we give Phone()
way to add an empty routing attributes, which means that users only need to access /about
, without the need to specify the operation can access this method. For the country
method, we specify in the routing properties country
Under Save AboutController.cs
, refresh your browser and visit /about
, we can see that the normal output of the phone number
If we visit /about/country
, then this will access AboutController
controller Country()
method
If you want some URL contains the name of our control, then we can not be displayed directly specify the name of the controller, the controller Instead of using a token in square brackets, which is used to tell the ASP.NET MVC use at this location the name of this controller
As shown in the following procedure
using System;
using Microsoft.AspNetCore.Mvc; namespace HelloWorld.Controllers { [Route("[controller]")] public class AboutController { public AboutController() { } [Route("")] public string Phone() { return "+10086"; } [Route("[action]")] public string Country() { return "中国"; } } }
This way, even if we rename the router, there is no need to change the route. The same is true for an action, and implicitly between the controller and the operation has a slash ( /
). It is a hierarchical relationship between the controller and action, just as it did in the URL
Once again, we save the AboutController.cs
file and re-run the application, we will see the same results
If we visit /about/country
, then this will access AboutController
controller Country()
method