Getting Started: ASP. net mvc Routing
As a developer who transfers data from ASP. NET to ASP. net mvc, you may have started using routes when developing ASP. NET websites. However, in ASP. net mvc, routing is a key part, and must be added to ASP. NET. Next we will learn about the routing system in ASP. net mvc.
I. Preparations
1. Create an ASP. NET MVC4 Project
2. The template is empty.
3. Add a Home controller to Controllers.
4. Open the RouteConfig file in App_Start.
5. Delete the default route in RouteConfig
Ii. Getting Started
1. Basic Routing
RouteConfig. cs:
Run the website, enter http: // localhost: xxxx/Home/Index, and press Enter. Then you can see the corresponding page. Of course, you will think this is very simple, but we also need to understand how this is done.
The following shows the comparison between the URL path and the path in the route Configuration:
From the figure above, we can get the following set of Route data:
Then the Controller factory will call the Index method in HomeController based on these parameters. how this works will be explained later in the Controller factory. After mastering the above basics, we can continue to learn.
2. Default route
In the above example, we will find that an exception is reported when the website is opened by default, but we need to enter a domain name to access all website values, and then we can see the home page, then how do we go to ASP. net mvc to achieve this effect, we will introduce it below.
RouteConfig. cs
Tip: the default variable name in default must be the same as the name in url, but the case is not limited.
Then we can re-open it to see the correct results. If you do not use F5 to view the data, you need to recompile the project and refresh the browser.
3. Static URL fragments
Maybe someone has tried to input some characters out of curly braces, which is actually allowed. In this way, you can create a more beautiful path. For example, we can change the route to the following:
Through the URL above, we can see that the input path should be like this: http: // localhost: 2392/XHome-Index (:-) is not very personalized ), if you are a researcher, you will find that after removing the url x, you will not be able to display the default homepage, but "/" is acceptable. Of course, this is ASP. the core of net mvc can be completely rewritten later.
4. Custom fragment Variables
If you are a person who likes to draw images from the gourd tree, you will certainly add another curly braces in the URL. Of course, you are doing well, but do you understand where the values captured by curly brackets go? If you are proficient in ASP. NET and add RouteData to the Controller, you can get these values, but this is not the result we want. Next we will learn more deeply.
First, modify the content of RouteConfig. cs:
Then we must enter the following path to see the page: http: // localhost: 2392/Home/Index/1. You can find that the last one is more than one, this 1 was captured by {id} and stored in RouteData. Here we can use other methods to obtain all captured values in the url.
Open HomeController. cs and modify the Index as follows:
Next, we modify the content in Views/Home/Index. cshtml to display the captured variables:
Then refresh the browser to see the following results:
You will find that the parameter name of the Index method must be the same as the name in the curly brackets of the route, otherwise it will not be captured (if you have learned a lot of ASP.. net mvc can be customized ).
We can find that the home page cannot be opened directly, because {id} has become a required parameter. We can solve this problem in two ways.
(1) set the default value:
After selecting one of the methods above, we can directly open the home page.
Tip: After changing {id} to {* id}, We can get all the values entered after http: // localhost: 2392/Home/Index.
5. Ambiguity
If we create a Home controller in Models, you will find an error after refresh. This is because you cannot determine which controller to choose to respond to the request. Of course, you think that as long as we do not create a new controller with the same name, in this way, you can only control the appearance of your project, but you cannot control the appearance of the class library you load, but ASP. net mvc has provided a solution for us to correct RouteConfig as follows. cs:
We can see the namespaces parameter. By passing the namespace name in, we can eliminate the ambiguity.
6. constraint Routing
The above {id} is used to capture parameters, but you also find that it can capture any string and so on, but we sometimes need to limit it, for example, to allow it to only enter numbers, then we can use a regular expression to constrain it.
Modify RouteConfig. cs as follows:
We can clearly see that we use the constraints parameter to restrict the id parameter to only numbers. Of course, you can also constrain other parameters in the same way. You can use httpMethod = new HttpMethodConstraint ("GET", "POST") to restrict access to this route only in that way.
If your constraints cannot be met, the following custom constraints will certainly meet your requirements. To create a custom constraint, you only need to create a class that implements the IRouteConstraint interface. For example, we can only access the restriction from Google browsers, create a new Filters file, and create a new MyRouteConstraint class, write the following code:
Next we switch to different browsers for testing, and we will find the difference (if you modify the UserAgent using browser developer tools or other tools, you can also bypass this constraint, such as Filddler2)