. Text Urlrewrite Introduction.

Source: Internet
Author: User
Tags config httpcontext regular expression
1, Tidbits:
The first time I got Dottext, it was strange to me that

First, to floerggyy registered through the URL:HTTP://X.X.X.X/FLOERGGYY can enter their blog
(In fact, forget the previous often do download page download.aspx is only processing the HttpHandler of the virtual page, it may be seen in.) Text excited even these basic common sense have forgotten ^_^)

Two, actually can take the user name to do the user's unique identification but does not find in the table to do as the user name username the unique constraint the Dongdong (to now still do not know which place in the database is set, has the knowledge to please instruct under)
Later, through the registration of the same user name to see the exception information thrown to confirm that there is indeed a username as the only constraint.
Alas, I don't seem to know anything about the database.


... Later decided to write an article on the URL rewrite, and then see Dottext the original author also briefly introduced the next urlrewrite, so the idea gave up.
Later, some friends asked Dottext about the URL of the problem, it seems to write it
2, configuration file Webconfig.config simple browsing
Customizing configuration section content:
<configSections>
<section name= "blogconfigurationsettings" type= "Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.framework "/>
<section name= "handlerconfiguration" type= "Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.framework "/>
<section name= "searchconfiguration" type= "Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.framework "/>
<section name= "microsoft.web.services" type= "Microsoft.Web.Services.Configuration.WebServicesConfiguration, Microsoft.Web.Services, version=2.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35 "/>
<section name= "Codehighlighter" type= " ActiproSoftware.CodeHighlighter.CodeHighlighterConfigurationSectionHandler, Actiprosoftware.codehighlighter "/ >
</configSections>

HttpHandler Configuration content:
<add verb= "*" path= "*.asmx" type= "System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, version=1.0.5000.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a "
Validate= "false"/>
<add verb= "*" path= "error.aspx" type= "System.Web.UI.PageHandlerFactory"/>
<add verb= "*" path= "*" type= "Dottext.common.urlmanager.urlrewritehandlerfactory,dottext.common"/>

HttpModule Configuration content:
<add name= "Urlrewritemodule" type= "Dottext.Common.UrlManager.UrlReWriteModule, Dottext.common"/>
<add name= "Eventhttpmodule" type= "Dottext.Framework.ScheduledEvents.EventHttpModule, Dottext.framework"/>



See a strange project first open its profile look, this is my habit:
Let's take a look at some of the key configuration elements:
Read the above content in Web.config familiar with the asp.net running mechanism of friends understand, Dottext code to run the order. Here, I'll simply repeat.
The internal operating mechanism of the ASPNET (if you have unfamiliar friends see <<asp.net famework Depth Adventure >> This book, it is helpful to do asp.net development friends):
Remote client Request----&GT;IIS---->aspnet_isapi.dll-->aspnet_wp.exe-->httpruntime--->
HttpModule--->httphandler Factory--->httphandler--->httphandler.processrequest ()-->response Client Request
Well, the problem is turning, client request is first intercepted by HttpModule. When we ask for the url:http://www.cnblogs.com/floerggyy/of. Text, the first is
The related methods of class Urlrewritemodule under the Dottext.Common.UrlManager namespace are invoked.
(Why would a remote request be intercepted by a class urlrewritemodule?) The contents of the above HttpModule configuration section are not marked??? ^_^
Ask, then will the class Eventhttpmodule under the Dottext.Framework.ScheduledEvents namespace intercept the remote request? When did you intercept it?
Of course, it is in the first order, China's fine tradition has forgotten!!!
(In fact, this is not very accurate, the two HttpModule are indeed in order, but in the HttpModule in some of the events they are cross, good class Eventhttpmodule
Not within the scope of our account in the following code is not analyzed, there is not clear to this piece of the best to see the above recommended book ^_^)
3, URL Rewrite, part of the Code analysis (this piece involves many custom configuration section, HttpModule, HttpHandler comprehensive application so to straighten out or a little trouble, to have a small point of analysis of the patience of others code. Personally think)
The method of class Urlrewritemodule

private void Context_beginrequest (object sender, EventArgs e) {
It is the primary role to set whether to rewrite the URL requested by the client (it defaults to the URL), depending on the request matching the regular expression, note that the code urlhelper.setenableurlrewriting (Context,false);
if (Configprovider.instance (). Isaggregatesite) {
HttpContext context = ((HttpApplication) sender). context;

String path = context. Request.Path.ToLower ();
int iextrastuff = path. IndexOf (". aspx");
if (Iextrastuff > 1 | | | path. IndexOf (".") = = 1) {
if (Iextrastuff >-1)
{
Path = path. Remove (Iextrastuff+5,path. Length-(iextrastuff+5));
}

Path = Regexapplication.replace (path,string. empty,1,0);

if (Path = = "" | | Path = = "/" | | Regexpath.ismatch (PATH))
{
Urlhelper.setenableurlrewriting (Context,false);
}

}else if (context. Request.Path.ToLower (). IndexOf ("services") > 0 && context. Request.Path.ToLower (). IndexOf (". asmx") > 0)
{
if (Alllowservice (context))
{
if (context. request.requesttype!= "POST")
{
String regexstr=@ "/\w+/services/";
String Url=regex.replace (context. Request.rawurl,regexstr, "/services/", regexoptions.ignorecase);
Context. RewritePath (URL);
}
String FileName =context. Request; System.IO.Path.GetFileName (context. Request.path);
Context. RewritePath ("~/services/" + fileName);
}else{
Context. Response.Clear ();
Context. Response.End ();
}
}

}



HttpModule after processing (this sentence is not correct, here is the case) into the HttpHandler Factory, according to HttpHandler configuration content we can immediately find this class
Urlrewritehandlerfactory It is processing rewrite URL request core, here I analyze it in detail.
It implements the IHttpHandlerFactory
(Note that this class is important)

HttpModule after processing (this sentence is not correct, here is the case) into the HttpHandler Factory, according to HttpHandler configuration content we can immediately find this class
Urlrewritehandlerfactory It is processing rewrite URL request core, here I analyze it in detail.
It implements the IHttpHandlerFactory
(Note that this class is important)
Using System;
Using System.Web;
Using System.Web.UI;
Using System.Text.RegularExpressions;

Using Dottext.framework;
Using Dottext.Framework.Components;
Using Dottext.Framework.Configuration;

Namespace Dottext.Common.UrlManager
{
/**////<summary>
Class responisble for figuring out which. Text page to load. By default would load an array of Dottext.UrlManager.HttpHanlder
From the Blog.config file. This is contains a list of Regex patterns to match the current request to. It also allows caching of the
Regex ' s and Types
</summary>
public class Urlrewritehandlerfactory:ihttphandlerfactory
{
Public urlrewritehandlerfactory () {}//nothing to does in the CNSTR
Construct HttpHandler when customizing virtual methods to deserialize from custom configuration section content
Protected virtual httphandler[] Gethttphandlers (HttpContext context)
{
Return Handlerconfiguration.instance (). httphandlers;
}

/**////<summary>
Implementation of IHttpHandlerFactory. By default, it would load an array of Httphanlder (Dottext.UrlManager.HttpHandler) from
The Blog.config. This can is changed by overrideing the Gethttphandlers (HttpContext context) method.
</summary>
<param name= "Context" >current httpcontext</param>
<param name= "RequestType" >request Type (passed along to other IHttpHandlerFactory ' s) </param>
<param name= "url" >the current requested URL. (Passed along to IHttpHandlerFactory ' s) </param>
<param name= "path" >the physical path of the current request. Is isn't gaurenteed to exist (passed along to other IHttpHandlerFactory ' s) </param>
<returns>
Returns a Instance of IHttpHandler either by loading a Instance of IHttpHandler or by returning
Ihttphandlerfactory.gethanlder (HttpContext context, string RequestType, string URL, String path) method
</returns>
Methods of implementing Interface IHttpHandlerFactory definition
Public virtual IHttpHandler GetHandler (HttpContext context, string RequestType, string URL, string path)
{
Get the handlers to process. By Defualt, we grab them from the Blog.config
httphandler[] Items = gethttphandlers (context);
Dottext.Framework.Logger.LogManager.Log ("path", Dottext.Framework.Util.Globals.RemoveAppFromPath) (context. Request.path,context. Request.applicationpath));
Do we have any?
if (items!= null)
{
int count = items. Length;

for (int i = 0; i<count; i++)
{
We should use our own cached Regex. This should limit the number of Regex ' s created
and allows us to take advantage of regexoptons.compiled
Match the type of request defined in the configuration section individually
if (items[i). IsMatch (Dottext.Framework.Util.Globals.RemoveAppFromPath) (context. Request.path,context. Request.applicationpath)))
{
Note Here is the key, note the return of the HttpHandler instance
throw new Exception ();
Switch (items[i). Handlertype)
{
Case handlertype.page://Default is Page

Return Proccesshandlertypepage (Items[i],context,requesttype,url);
Case Handlertype.direct:
Handlerconfiguration.setcontrols (Context,items[i]. Blogcontrols);
Return (IHttpHandler) items[i]. Instance ();
Case Handlertype.factory:
Pass a long of the request to a custom ihttphandlerfactory
Return ((IHttpHandlerFactory) items[i]. Instance ()). GetHandler (Context,requesttype,url,path);
Default
throw new Exception ("Invalid handlertype:unknown");
}
}
}
}
If we don't find the page and just let asp.net take over
Return Pagehandlerfactory.gethandler (Context,requesttype,url, path);
}


Private IHttpHandler Proccesshandlertypepage (HttpHandler Item, HttpContext context, string RequestType, string url)
{
String Pagepath = Item. Fullpagelocation;
if (Pagepath = null)
{
Pagepath = Handlerconfiguration.instance (). Fullpagelocation;
}
Handlerconfiguration.setcontrols (Context,item. Blogcontrols);
IHttpHandler myhandler=pageparser.getcompiledpageinstance (Url,pagepath,context);
return MyHandler;
}


public virtual void Releasehandler (IHttpHandler handler)
{

}
}
}

Notice how it httphandler the contents of the Custom configuration section to synthesize the instance of the
Straighten these out for understanding. Text URL Rewriting is not difficult ....
A welcome master who has an understanding of the above solution

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.