asp.net url rewrite simplified version quick URL Rewrite _ practical tips

Source: Internet
Author: User
Tags httpcontext
One of the easiest ways to implement URL rewriting (urlrewriter) in asp.net.
Reference (author Scott Mitchell translation: Janssen) of the masterpiece, although not fully understand, but also suit did a, quite "achievement" sense. Write it out and share it.
The original said a lot of principles, here is not to say (in fact, I do not understand). Here's how to write the procedure. The goal is to implement one of the simplest programs that can implement URL rewriting.
1, need to set the site properties in IIS.

2, modify the content of Web.config.

Copy Code code as follows:

<system.web>
<add verb= "*" path= "*.zhtml" type= "Zdil". Urlrewriter.rewriterfactoryhandler, Zdilurlrewriter "/>
</system.web>


Where *.zhtml is the address bar to write the extension of the page, is to show the user, this can be changed at will (but to comply with the extension of the rules!) )。 Of course, it should be consistent with the settings in the first step.
3, write a class.

Code
Copy Code code as follows:

Using System;
Using System.IO;
Using System.Web;
Using System.Web.UI;
Namespace Zdil. Urlrewriter
{
/**////<summary>
URL Rewrite
</summary>
public class Rewriterfactoryhandler:ihttphandlerfactory
{
/**////<summary>
GetHandler is executed by the ASP.net pipeline after the associated httpmodules run. The job of
GetHandler is to return a instance of an HttpHandler that can process the page.
</summary>
<param name= "Context" >the HttpContext to 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 are the same class that the default ASP.net pagehandlerfactory delega Tes
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 you really want to visit
String querystring = ""; Parameters. Like id=123?
FilePath = context. Server.MapPath (sendtourlstring); Physical Address
This is the most important sentence. Turned away.
Context. RewritePath (Sendtourlstring, String.Empty, querystring);
Return pageparser.getcompiledpageinstance (URLs, FilePath, context);
}
public virtual void Releasehandler (IHttpHandler handler)
{
}
}
}


This class is written in a separate project and then compiled into a ZDILURLRewriter.DLL file. (Note the filename, the error is not working correctly).
4, finished.
Open IE, enter http://.../1.zhtml in the Address bar.
The viewer sees the address of a static page, but it actually accesses the Dynamic Web page/web/index.aspx.
How simple it is.
Of course, this is the simplest, simple to the "no use" of the point. Because he would "rewrite" all the *.zhtml's visits to/web/index.aspx.
As for what page to rewrite to which page, here is not introduced (here only the method, do not talk about the implementation of the details).
A lot of ways, the original is through the regular to match, I was through the 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.