System. Web. RoutingAlready used asProgramSet included in. Net3.5sp1Released. Although we are not3.5sp1FoundAsp.net MVCBut it is not far from us.
System. Web. RoutingUsed inAsp.netOfWebIn the ApplicationUrlrouting.
The so-called urlrouting is to map an address to another address. For example, I accessed/chsword/2008/08/27. html and actually accessed/chsword/Article. aspx? Y = 2008 & M = 08 & D = 27. urlrouting makes our URL look pretty.
This article will be divided into two parts to introduce the use of urlrouting for the first and advanced sections respectively.
ArticlePrerequisites:
1.InstallVisual Studio 2008 SP1Or otherIDE.
2.CreateAsp.net Web Application(Asp.net WebApplication)
3.ReferenceSystem. Web. RoutingAssembly.
Urlrouting Implementation Principle
If you are not the person pursuing the theory, you can simply start from the preparation work.
For your convenience, I have drawnUMLFigure.RoutetableOfRoutesYou can obtainRoutecollectionOfSingletonMode, which is initialized only when the value does not exist.SingletonBut it is inApplication_startEvents are initialized until the application process ends, so yesSingletonMode.
In the following waysRouteAddRoutetable. RoutesMedium
Routetable.Routes.Add(New route ());
AboveCodeThis Code cannot be correctly executed because route does not provide a non-argument constructor.
RouteDuring initializationRoutevaluedictionaryTo add default values and rulesRouteMedium
In additionRouteThere is anotherIroutehandlerImplementation object,IroutehandlerThe implementation object providesGethttphandlerMethod to obtain for processingURLOfIhttphandler.
This is still at the abstract level. Below we will take some simple examples to bring you upUrlrouting.
Preparations
A processing request is required.URLOfIhttphandlerSo we first define an implementationIhttphandlerInterface Class.
It is calledMypageBecause we wantIroutehandlerInteraction,In additionIhttphandlerTo declareRequestcontextType attribute.
Public Class Mypage: ihttphandler {
Public Requestcontext { Get ; Private Set ;}
Public Mypage (requestcontext context)
{
This . Requestcontext = Context;
}
# Region Ihttphandler Member
Public Virtual Void Processrequest (httpcontext context ){}
Public Bool Isreusable {
Get { Return False ;}
}
# Endregion
}
In this way, we have our ownIhttphandler.
Below we implementIroutehandler:
Public Class Myroutehandler: iroutehandler {
# Region Iroutehandler Member
Public Ihttphandler gethttphandler (requestcontext ){
Return New Mypage (requestcontext );
}
# Endregion
}
Implemented hereIroutehandlerOfGethttphandlerMethod to returnMypage.
In this way, we have finished the first two jobs. We can achieve this.Urlrouting.
Implement the first Urlrouting
ActuallyUrlroutingAfter defining the above two rules, it is easy.
InGolbal. asax(No one can be created)Application_startWrite the following code in the event:
Protected Void Application_start ( Object Sender, eventargs e ){
Registerroutes (routetable. routes );
}
Public Static Void Registerroutes (routecollection routes ){
Routes. Add ( New Route ( " {Page}. aspx " , New Myroutehandler ()));
}
In this way, we define the firstUrlroutingThe rule isXXXX. aspxSuchURLProceedRouting.
However, we only define what to deal.URLBut does not define how to handle it.
We shouldMypageOfProcessrequestMethod.
We willProcessrequestThe method is as follows:
Public Virtual Void Processrequest (httpcontext context ){
Context. server. Execute (requestcontext. routedata. Values [ " Page " ]. Tostring (). Replace ( " _ " , " / " ) + " . Aspx "
);
}
obviously, requestcontext . routedata. values [" page "] the rule is obtained {page }. the value of page In aspx is, if I access index. aspx then requestcontext . routedata. values [" page "] is index .
My definition here is"_"Replace"/"ThereforeList_index.aspxSuchURLGoLIST/index. aspxSuch a webpage.
We have created some web pages for testing, as shown in:
On these pages, you can freely write texts that can distinguish webpages.
AccessList_chsword.aspxAutomaticRoutingArrivedLIST/chsword. aspxOn.
To sum upUrlroutingRelated:
1.Application_startRules defined in
2.Self-implementedIhttphandlerClass
In this way, you are getting started with urlrouting. Next we will talk about some advanced settings in the future.
Expected content:
- Default Value of the rule
- Regular Rule settings
- Tips
System. Web. Routing