Asp.net pseudo-static (URLRewriter) Rewriting

Source: Internet
Author: User

The simplest method to implement URL rewriting (URLRewriter) in asp.net.

1. Set site properties in IIS.

2. modify the content of web. config.
<System. web>
<HttpHandlers>
<Add verb = "*" path = "*. zhtml" type = "ZDIL. URLRewriter. RewriterFactoryHandler, ZDILURLRewriter"/>
</HttpHandlers>
</System. web>
*. Zhtml is the extension of the webpage written in the address bar, which is displayed to users. This can be changed at Will (but it must comply with the extension rules !). Of course, it must be consistent with the settings in step 1.

3. Write a class.
Using System;
Using System. IO;
Using System. Web;
Using System. Web. UI;

Namespace ZDIL. URLRewriter
{
/** // <Summary>
/// URL rewriting
/// </Summary>
Public class RewriterFactoryHandler: IHttpHandlerFactory
{
/** // <Summary>
/// GetHandler is executed by the ASP. NET pipeline after the associated HttpModules have run. The job
/// GetHandler is to return an instance of an HttpHandler that can process the page.
/// </Summary>
/// <Param name = "context"> The HttpContext for this request. </param>
/// <Param name = "requestType"> The HTTP data transfer method (<B> GET </B> or <B> POST </B>) </param>
/// <Param name = "url"> The RawUrl of the requested resource. </param>
/// <Param name = "pathTranslated"> The physical path to the requested resource. </param>
/// <Returns> An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
/// By the <B> PageParser </B> class, which is the same class that the default ASP. NET PageHandlerFactory delegates
/// To. </returns>
Public virtual IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated)
{
String sendToUrl = url; // address in the address bar
String filePath = pathTranslated;

String sendToURLString = "/web/index. aspx"; // the page to be accessed
String queryString = ""; // parameter. For example? Id = 123

FilePath = context. Server. MapPath (sendToURLString); // physical address

// This sentence is the most important. Turning.
Context. RewritePath (sendToURLString, String. Empty, queryString );

// This is not clear yet :)
Return PageParser. GetCompiledPageInstance (url, filePath, context );
}

Public virtual void ReleaseHandler (IHttpHandler handler)
{// This does not understand either
}
}
}
This class should be written in a separate project and then compiled into the ZDILURLRewriter. DLL file. (Note the file name. If the file name is incorrect, it cannot run properly ).

4. Finished.
Open IE and enter http: //.../1. zhtml in the address bar.

The viewer sees a static page address, but actually accesses the dynamic page/web/index. aspx.

It's easy.

Of course, this is the simplest way to be "unusable. Because it will "Overwrite" All *. zhtml access to/web/index. aspx. As for what kind of web page is rewritten to which web page, we will not introduce it here (Here we only talk about methods, not the implementation details ). There are many methods. The original works are matched by regular expressions. I used string sendToUrl = url; to judge.
The rest depends on your needs.

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.