Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the scenario for choosing URL paths in the ASP.NET MVC framework?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail about the selection of URL paths in the ASP.NET MVC framework. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Path selection scenario in ASP.NET MVC framework: custom query URL

Let's use the custom path selection rules in the real scene to demonstrate this, and take the query function of our e-commerce website as an example.

To begin, let's 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 that allows the user to enter and submit query text. The Results () action method is used to process the corresponding form submission, query the database, and then display the results to the user:

Using the default / [controller] / [action] / [id] URL path mapping rules, we can readily use URL like this to invoke our SearchController behavior:

Note that the default mapping of the root URL / Search to the Index () action method is because when Visual Studio creates a new project, the default path definition of / [controller] / [action] / [id] automatically sets the default action to the "Index" (through the Defaults property):

Although a URL like / Search/Results?query=Beverages is perfectly feasible, we may decide that we want a slightly better looking URL for the query results. Specifically, we might want to remove the "Results" action name from URL and pass in the text to be queried as part of URL, rather than as the value of URL's query string. For example:

We can enable these good-looking query results URL by adding two custom URL path mapping rules before the default / [controller] / [action] / [id] rule, as follows:

In the first two rules, we now explicitly specify the controller and Action parameters corresponding to / Search/ URL. We show that "/ Search" should always be handled by the "Index" action on SearchController. Any URL (/ Search/Foo, / Search/Bar, etc.) that owns a child URL hierarchy is always handled 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]", which is passed into the Results action method on SearchController as a method parameter:

Most likely, we will also enable paging for query results (we only display 10 query results at a time). We can do this by querying the string value (for example, / Search/Beverages?page=2), or we can embed the page number in the URL (such as / Search/Beverages/2). To support the latter approach, what we need to do is to add an additional saving parameter to our second path selection rule:

Notice that the new URL rule above now matches "Search/ [query] / [page]". We also configure the default page number to 1 in case the page number is not included in the URL (this is passed in through an anonymous type as the value of the "Defaults" attribute).

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 verification of 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 prerequisite rules that must be true (except for URL filtering) in order for path selection rules to match. The asp.net mvc framework allows you to use regular expressions to validate parameter values in URL, as well as to evaluate HTTP Headers (choose different URL paths according to different HTTP verbs).

Here is a custom validation rule that we can use on URL like / Products/Detail/43, which specifies that the ID parameter must be a number (strings are not allowed) and that it must be between 1 and 8 in length:

If we pass a URL like / Products/Detail/12 into the application, the above path selection rule is legal, but if we pass in / Products/Detail/abc or / Products/Detail/23232323232, it will not match.

On the ASP.NET MVC framework of the URL path selection scenario is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report