asp.net address mapping-pseudo-static

Source: Internet
Author: User
Keywords Pseudo static

Intermediary transaction SEO diagnosis Taobao guest Cloud host technology Hall

asp.net address mapping is defined as the actual file of the virtual address map that the user accesses, and the virtual address may be a static page. For example: In the visit to the Http://fuzhou.naoqiu.com/bus page, actually visit the/bus.aspx page.

asp.net address mapping is done through the HttpModule request principle.

What is the HttpModule principle?

An HTTP request passes the HTTP request to the HttpHandler container at a time (Resolverequestcache event) during the delivery of the HttpModule container. After this event, the HttpModule container creates a HttpHandler entry instance, but the HTTP request control is not surrendered at this time. Instead, it continues to trigger acquirerequeststate events and Prerequesthandlerexcute events. After the Prerequesthandlerexcute event, the HttpModule window temporarily hands control to the HttpHandler container for real HTTP request processing.

The ProcessRequest method is executed inside the HttpHandler container to process the HTTP request. After the container HttpHandler processes the entire HTTP request, the control is returned to Httpmodule,httpmodule, which continues to layer the forwarding of the processed HTTP request flow until it is returned to the client.

  

Borrow pictures from Bo friends

After I understand the principle, I'll complete a simple address map with the following code:

First complete the address map base class:

<summary>

Address Rewriting

</summary>

Public abstract class Basemodulerewriter:ihttpmodule

{

<summary>

Initialization call

</summary>

<param name= "App" ></param>

public virtual void Init (HttpApplication app) {

App. BeginRequest + = new EventHandler (app_beginrequest);

}

public virtual void Dispose () {}

<summary>

Request Start

</summary>

<param name= "Sender" ></param>

<param name= "E" ></param>

protected virtual void App_beginrequest (object sender, EventArgs e)

{

HttpApplication app = sender as HttpApplication;

Rewrite (APP);

}

<summary>

Abstract method of operation

</summary>

<param name= "App" ></param>

protected abstract void Rewrite (HttpApplication app);

}

Inheriting base class, completing the need for address mapping operations:

protected override void Rewrite (HttpApplication app)

{

HttpContext context = App. context;

HttpRequest request = App. Request;

String part = Regex.match (Request. Url.tostring (), @ "(? <=http\:\/\/). *"). Value.tolower (). Trim ('/');

if (part. IndexOf ("/") = = 1)

{

Part = Regex.match (part, @ "^\w+"). Value;

Switch (part)

{

Case ' bus ': Context. RewritePath ("/bus.aspx"); Break;

Case "www":

Case "Naoqiu": the context. RewritePath ("/default.aspx"); Break;

Default:context. RewritePath ("/index_city.aspx"); Break;

}

}

}

So what are we going to do? Yes, it's a configuration file. There are several places to modify Web.config files, such as:

<!--request Rules-->

<httpModules>

<add name= "Bianminmodulerewriter"
Type= "Bianmin.urlrewriter.modulerewriter,bianmin.urlrewriter"/>

</httpModules>

<system.webServer>

<handlers>
<add name= "Aspnet_isapi" path= "*" verb= "*" modules= "isapimodule" script processor= "C:\Windows\Microsoft.NET\ Framework64\v4.0.30319\aspnet_isapi.dll "resourcetype=" Unspecified "requireaccess=" None "precondition=" Classicmode,runtimeversionv4.0,bitness64 "/>
</handlers>

<defaultDocument>

<files>

<clear/>

<add value= "default.aspx"/>

</files>

</defaultDocument>

</system.webServer>

Note: When you use Framewore 2..0 you need to change the \aspnet_isapi.dll file path

Source code Download Address: http://download.naoqiu.com download

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.