Asp.net route configuration 2

Source: Internet
Author: User

1: register the route mode you write in application_start.CodeAs follows:
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 rulesProgramMedium
Registerroutes (routetable. routes );

}

Public static void registerroutes (routecollection routes)
{
// Add a route to the program
// New route ("{page }. aspx ", new myroutehandler () d parsing," {page }. aspx ", the first parameter is the request path, and the second parameter is how to process the request.
// Here, myroutehandler is used to process the route request.

// Add another route
Routes. Add ("default ",
New route ("{category}/{action}. aspx ",
New routevaluedictionary (
New
{
File = "default ",
Category = "home ",
Action = "Index"
}), New myroutehandler () // define a routehandler, and use this handler to get the Request Path
)
);

}
2: Define a routehandler to process the above request path
Public class myroutehandler: iroutehandler
{
# Region iroutehandler Member
Public ihttphandler gethttphandler (requestcontext)
{
// Return a request object (ihttphander), that is, the requested page object. obtain the information of the current request object in mypage and process the request logic.
Return new mypage (requestcontext );
}
# Endregion

}
3: Define an ihttphandler implementation object mypage
Public class mypage: ihttphandler
{

Public requestcontext {Get; set ;}

Public mypage (requestcontext context)
{
This. requestcontext = context;
}

# Region ihttphandler Member

// The URL method used to process the request, that is, to replace the corresponding parameter in the Request Path
Public Virtual void processrequest (httpcontext context)
{

Context. server. Execute (string. Format ("/{0}. aspx? Category = {1} & Action = {2 }",
Requestcontext. routedata. Values. containskey ("file ")
? Requestcontext. routedata. Values ["file"]. tostring ()
: "Default ",
Requestcontext. routedata. Values. containskey ("category ")
? Requestcontext. routedata. Values ["category"]. tostring ()
:"",
Requestcontext. routedata. Values. containskey ("action ")
? Requestcontext. routedata. Values ["action"]. tostring ()
:"")
);

}

Public bool isreusable
{< br> get {return false ;}< BR >}< BR ># endregion
}< br>
4: and in the web. system. configure webserver nodes as follows






after completing the preceding route configuration, theoretically, the route configuration has been completed.
the access path/a/B/defaul. aspx is used to access default. aspx? Category = A & Action = B path, but the following error is reported when I run the program. Thanks for your help!
error page:

Related Article

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.