Although it was finally implemented, as a programmer, I always felt that I could not grasp the core of the technology by using other components that are not open source. In fact, in net, Microsoft has left an interface for us to do whatever we want.
First, we can take a picture to understand the. net lifecycle.
As you can see, the server creates a new HttpContext instance for each request of different users until the request ends and the server destroys the instance. Ihttpcontext is an interface made public by httpcontext. It contains two methods: dispose () and Init (HttpApplication context). We can implement Ihttpcontext to achieve httpcontext.
Key code:
Copy codeThe Code is as follows:
HttpApplication app = (HttpApplication) sender;
HttpContext context = app. Context;
String url = context. Request. Url. AbsoluteUri; // complete url
String turl = url. Split ('.') [0];
String surl = turl. ToLower (). Replace ("http ://","");
StringBuilder strb = new StringBuilder ();
Strb. Append (url );
Strb. Append (surl );
App. Context. RewritePath (path, string. Empty, strb. ToString (). Split ('? ') [1]);
In web. config, configure the following:
Add the following code in <system. web>.
<HttpModules>
<Add type = "Common. URLRewriter" name = "Common"/>
When setting IIS, remember to set the IIS header to null.
You can achieve this by running
</HttpModules>