In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the ASP.NET MVC framework URL path selection rules, which may not be well understood by many people. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope everyone can gain something according to this article.
Here, we create an e-commerce site that presents three types of URLs:
We handle these URLs by creating a ProductsController class like this:
After adding the above class to our application, the ASP.NET mvc framework automatically directs incoming URLs to the appropriate action methods on our controller to process the request.
In today's post, we'll discuss in depth how this URL mapping happens and explore more advanced routing scenarios that we can leverage within the ASP.NET mvc framework. I'll also demonstrate how you can easily unit-test URL path selection scenarios.
What does asp.net's URL routing system do?
The asp.net mvc framework includes a flexible URL routing system that allows you to define URL mapping rules in your application. The routing system has two main purposes:
Map incoming URLs to applications and direct them so that the correct Controller and Action methods execute to handle these requests
Build URLs that you can use to call back the output of Controllers/Actions to the client (for example, form submission,
< a href="">links, and AJAX calls, etc.)
Being able to use URL mapping rules to handle both incoming and outgoing URL scenarios adds a lot of flexibility to application code. This means that if we want to change the URL structure of our app later (for example, changing/Products to/Catalog), we can modify a set of mapping rules at the app level without changing any code in the controller or view template.
Default URL path selection rules for ASP.NET mvc
By default, when you use Visual Studio to create a new project with the ASP.NET mvc Web Application template, it adds an ASP.NET Application class to the project. This is implemented in the Global.asax background code:
asp.net The Application class allows developers to handle the logic of application startup/abort and global error handling.
The default ASP.NET mvc project template automatically adds an Application_Start method to this class, registering two URL routing rules in it:
The *** path selection rule above indicates that the ASP.NET mvc framework, by default, should map URLs to controllers using the format "[controller]/[action]/[id]" when deciding which Controller class to use to generate instances, which Action methods to invoke (and which parameters to pass in).
This default routing rule is why requests for URL /Products/Detail/3 in our eCommerce browsing routines in section *** automatically invoke the Detail method of our ProductsController class, passing in 3 as the id parameter value:
The second routing rule above is an exception to our application's root URL"Default.aspx"(this URL is sometimes passed in by the server instead of "/" when processing requests for an application's root URL). This rule ensures that requests to the root "/Default.aspx" or "/" of our app are handled by the Index() action method in the HomeController class, which is automatically generated by Visual Studio when we build a new app using the ASP.NET mvc Web Application project template.
Understanding Route Examples
Route selection rules are registered by adding Route instances to the Routes collection of System.Web.mvc.RouteTable.
The Route class defines a number of properties that you can use to configure mapping rules. You can set these properties in the "traditional". net 2.0 property assignment way:
Or take advantage of the new object initializer feature in VS 2008's C#and VB compilers to set properties more concisely:
The Url attribute of the Route class defines Url matching rules that should be used to evaluate whether a routing rule applies to a particular incoming request. It also defines how the URL should be tokenized into different parameters. The replaceable parameters in the URL are defined by the syntax of [parameter name]. As discussed later, we are not limited to a fixed set of "familiar" parameter names; you can use any number of arbitrary parameters in the URL. For example, I can split the URL of an incoming blog post using a URL rule of "/Blogs/[Username]/Archive/[Year]/[Month]/[Day]/[Title]," which the mvc framework automatically parses into UserName, Year, Month, Day, and Title parameters and passes them into my controller's action method.
The Defaults attribute on the Route class defines a dictionary of default values that can be used if the incoming URL does not contain a specified parameter value. For example, in the URL mapping example above, we defined two default URL parameter values, one "[action]" and the other "[id]." This means that if the app receives the URL/Products/, the routing system will default to using "Index" as the name of the ProductsController action. Similarly, if/Products/List/is specified, the null string is used as the value of the "ID" parameter.
The RouteHandler attribute of the Route class defines the IRouteHandler instance that should be used to process the request after the URL has been split into parameters and the appropriate routing rules have been determined. In the example above, we indicated that we wanted to use the System.Web.mvc.mvcRounteHandler class to process our configured URL. The reason for this extra step is that we want to ensure that the URL routing system can be used for both mvc and non-mvc requests. Having this IRouteHandler interface means that we can also use it cleanly for non-mvc requests (e.g. standard WebForms, Astoria REST support, etc.).
The Route class also has a Validation attribute, which we'll discuss later in this article. This property allows us to specify the prerequisites that a path selection rule matches. For example, we can specify that a path selection rule should only apply to a particular HTTP verb (allowing us to easily map REST commands), or we can use regular expressions on parameter values to filter whether a path selection rule matches.
Note: In the first public preview of the ASP.NET mvc framework, the Route class was non-extensible (it's just a data class), and in the next preview we're working on making it extensible, allowing developers to add scene-specific path classes (e.g., a RestRoute subclass) to add new semantics and functionality cleanly.
Evaluation of path rules
When an incoming URL is received by the ASP.NET mvc Web application, the ASP.NET mvc framework evaluates the routing rules in the RouteTable.Routes collection to determine the appropriate Controller to handle the request.
asp.net The mvc framework evaluates which Controller to use in the order in which the RouteTable rules are registered. Each Route rule is checked against incoming URLs to see if it matches, and if a Route rule matches, that rule (and associated RouteHandler) is used to process incoming requests (all subsequent rules are ignored). This means that you generally organize your route selection rules in order of "most specific to least specific."
After reading the above, do you have any further understanding of what the URL path selection rules of ASP.NET MVC Framework are? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.