Path Selection scenario in ASP. net mvc Framework: Custom query URL
Let's use the custom path selection rules in actual scenarios to demonstrate this process. The following uses the query function of our e-commerce website as an example.
At the beginning, we will add a new SearchController class to our project:
Then, we define two Action methods in the SearchController class. The Index () method is used to display a query page with a text box on which users can enter and submit query text. The Results () action method is used to process the corresponding form submission, query the database, and display the result to the user:
Using the default/[controller]/[action]/[id] URL path ing rule, we can use a URL like the following to call our SearchController behavior:
Note that the root URL/Search is mapped to the Index () action Method by default because when Visual Studio creates a new project, the default path definition of/[controller]/[action]/[id] is used to automatically set the default action to the Defaults attribute on "Index ):
Although like/Search/Results? A URL such as query = Beverages is completely feasible. We may decide that we want a better-looking URL for the query results. Specifically, we may want to remove the "Results" action name in the URL and pass in the text to be queried as part of the URL, rather than as the value of the URL query string. For example:
We can enable these nice query result URLs by adding two custom URL path ing rules before the default/[controller]/[action]/[id] rules, as follows:
In the first two rules, the Controller and Action parameters of the corresponding/Search/URL are clearly specified. We show that "/Search" should always be handled by the "Index" action on the SearchController. All URLs/Search/Foo,/Search/Bar with sub-URL hierarchies are always processed by the "Results" action on the SearchController.
The second path selection rule above indicates that any character after the/Search/prefix should be treated as a parameter named "[query, this parameter will be used as a method parameter to pass in the Results action Method on SearchController:
Most likely, we also enable pagination for the query results so that only 10 query results are displayed at a time. We can query the string value, for example,/Search/Beverages? Page = 2), or we can embed the page number in the URL, for example,/Search/Beverages/2 ). To support the following method, we need to add an additional saving parameter to our second path selection rule:
Note that the new URL rule above matches "Search/[query]/[page]". We also set the default page number to 1. In case the page number is not included in the URL, this is passed in as the anonymous type of the "Defaults" attribute value ).
Then, we can update our SearchController. Results action Method to accept the page number parameter as a method parameter:
In this way, we have a better-looking query URL.
Prerequisites for verifying path selection rules in ASP. net mvc Framework
As I mentioned earlier in this post, the Route class has a Validation attribute that allows you to add rules to match the path selection rules, except for URL filtering ). The asp.net mvc framework allows you to use regular expressions to verify the parameter values in a URL. It also allows you to evaluate HTTP Headers and select different URL Paths Based on Different HTTP verbs ).
The following is a custom verification rule that can be used on a URL such as/Products/Detail/43. It specifies that the ID parameter must be a number and cannot be a string ), and its length must be between 1 and 8:
If we input a URL like/Products/Detail/12 to the application, the above path selection rule is valid, however, if/Products/Detail/abc or/Products/Detail/23232323232 is input, it will not match.
The above is the URL path selection scenario in the asp.net mvc framework.
- How to deploy the asp.net mvc program in IIS6.0
- Use Winform to build the asp.net mvc Framework
- Programming idea of ASP. NET Session failure
- ASP. NET Session state storage
- Understand ASP. NET Web Application Models