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: