Several classes about routing configuration in Asp.net (Asp.net route configuration 1)

Source: Internet
Author: User

1. routedatamsdn explains that it includes the relevant value of the requested route. Contains all the parameter values of the route mode you set.
Let's talk about several routedata attributes:
1: datatokensmsdn Interpretation -- obtain the information transmitted to the route for processing when the Asp.net route determines whether the route matches Program But the custom set is not used ..
That is to say, you get the route name of the route you configured, which matches the route name of the current route.
Code Is the best evidence of the problem: define the following route
Routes. mappageroute ("companysite_detail", "{path1}/{ID }",
"~ /Default. aspx ",
False,
New routevaluedictionary {"path1", "default" },{ "path2 ",""}},
New routevaluedictionary {"path1", "default" },{ "ID", @ "\ D + "}});
When the path/44/default is requested, it conforms to the route defined above. Use Page. routedata. datatokens to obtain a set of routevaluedictionary, and check that the value is "companysite_detail ".

2: Route msdn explanation -- get or set the route object. That is, all the information about the route you configured.
Routebase r = This. Page. routedata. route; A routebase object is returned. For routebase information, see msdn
Http://msdn.microsoft.com/zh-cn/library/system.web.routing.routebase.aspx

3: routehandler msdn explanation -- the returned result is the object that processes the route. This is just like our ihttphander. For ihttphander parsing, you can refer to what fish Li wrote.
Bytes.

4: values often uses a very important attribute msdn interpretation-get the URL parameter value and default value set of the route. This is a good understanding of all the parameters of your route.
This. Page. routedata. Values ["ID"]. tostring ();
Here is the value of the parameter ID. The returned value of routedata. values is also a set of routevaluedictionary, which contains path1 and path2.

2. Explanation of routetable class msdn -- storing the URL routing of an application. It is mainly used to register all routes into the program in the global. asax. CS file.
 
1: The property routes is a collection and is of the routecollection type. You can add all routes in the program to the next set.
Routes has a method: routes. mappageroute (string routename, string routeurl, string physicalfile, bool checkphysicalurlaccess, routevaluedictionary defaults, routevaluedictionary constraints );
This method has five override methods. Parameter 1 is the route name, parameter 2 is the routing mode, and parameter 3 is the physical path of the file, that is, the requested page path, parameter 4 is a value indicating ASP. net should verify whether the user has the right to access the physical URL (the routing URL is always checked ). This parameter sets system. Web. Routing. pageroutehandler. checkphysicalurlaccess. Parameter 5 sets the default value for the corresponding parameter, and parameter 6 sets the condition of the request parameter.

3. getrouteurl (string routename, routevaluedictionary routeparameters), an important method of using routes for the Page Object. This method is used to obtain related routes.
Parameter 1 is the name of the route you set, and parameter 2 is the value for the relevant routing parameters.
 
Take the following example:
 
Void application_start (Object sender, eventargs E)
{

// Register all the routing rules you set so that the corresponding rules are matched when you request the corresponding path. The routetable class stores the URLs of all the rules,
// Routetable. routes is a collection of routecollection, which stores all user-defined routing rules. The reason written in application_start method is to register all routing rules into the program.
Registerroutes (routetable. routes );

}

Public static void registerroutes (routecollection routes)
{
// Define a routing rule when the request/default/+ number is the request defaut page
Routes. mappageroute ("companysite_detail", "{path1}/{ID }",
"~ /Default. aspx ",
False,
New routevaluedictionary {"path1", "default" },{ "path2 ",""}},
New routevaluedictionary {"path1", "default" },{ "ID", @ "\ D + "}});

}

The parameter ID can be obtained in the Aspx. CS file as follows:
String Ss = This. Page. routedata. Values ["ID"]. tostring (); // obtain the parameter ID. You can query the relevant content in the database based on the SS.

On the page is Aspx. in this way, hyperlink2.navigateurl = page can be written in the CS background. getrouteurl ("companysite_detail", new {path1 = "default", path2 = 11}); set the Redirect path for a hyperlike instance to/defaut/44.

This route is complete. If you write something wrong, please make a selection !!!!!!!!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.