The easiest way to implement URL Rewriter in .
I have referenced the masterpiece (Translated by author Scott Mitchell: Janssen). Although I didn't fully understand it, I also made one according to the cat and made it, which is quite "achieving". Write it out and share it.
There are many principles in the original work, so I won’t talk about them here (actually I don’t understand them either). Let’s write the operation process here. The purpose is to implement the simplest program that can implement URL rewriting.
1. You need to set the site properties in IIS.
2. Modified content.
<>
<httpHandlers>
<add verb="*" path="*.zhtml" type=", ZDILURLRewriter" />
</httpHandlers>
</>
Among them, *.zhtml is the extension of the web page written in the address bar, which is for users to see. This can be changed at will (but it must comply with the rules of the extension!). Of course, it must be consistent with the settings in the first step.
3. Write a class.
Code
using System;
using ;
using ;
using ;
namespace
{
/**//// <summary>
/// URL rewrite
/// </summary>
public class RewriterFactoryHandler : IHttpHandlerFactory
{
/**//// <summary>
/// GetHandler is executed by the pipeline after the associated HttpModules have run. The job of
/// 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 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/"; //The page you want to visit
string queryString = ""; //Arguments. For example ?id=123
filePath = (sendToURLString); //Physical address
//This sentence is the most important. Turned.
(sendToURLString, , queryString);
return (url, filePath, context);
}
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
}
This class should be written in a separate project and then compiled into a file. (Note the file name, if written incorrectly, it will not run normally).
4. Completed.
Open IE and enter http://.../ in the address bar.
The viewer sees that it is the address of a static page, but in fact it is accessing the dynamic web page /web/.
How simple it is.
Of course, this is the simplest, so simple that it is "unusable". Because it will "rewrite" all *.zhtml accesses to /web/ .
As for what kind of web page to rewrite to which web page, I won’t introduce it here (only discuss the methods here, not the details of implementation).
There are many methods. The original work is matched through regularity. I judged it by string sendToUrl = url;.
The rest depends on your needs.
I have referenced the masterpiece (Translated by author Scott Mitchell: Janssen). Although I didn't fully understand it, I also made one according to the cat and made it, which is quite "achieving". Write it out and share it.
There are many principles in the original work, so I won’t talk about them here (actually I don’t understand them either). Let’s write the operation process here. The purpose is to implement the simplest program that can implement URL rewriting.
1. You need to set the site properties in IIS.
2. Modified content.
Copy the codeThe code is as follows:
<>
<httpHandlers>
<add verb="*" path="*.zhtml" type=", ZDILURLRewriter" />
</httpHandlers>
</>
Among them, *.zhtml is the extension of the web page written in the address bar, which is for users to see. This can be changed at will (but it must comply with the rules of the extension!). Of course, it must be consistent with the settings in the first step.
3. Write a class.
Code
Copy the codeThe code is as follows:
using System;
using ;
using ;
using ;
namespace
{
/**//// <summary>
/// URL rewrite
/// </summary>
public class RewriterFactoryHandler : IHttpHandlerFactory
{
/**//// <summary>
/// GetHandler is executed by the pipeline after the associated HttpModules have run. The job of
/// 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 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/"; //The page you want to visit
string queryString = ""; //Arguments. For example ?id=123
filePath = (sendToURLString); //Physical address
//This sentence is the most important. Turned.
(sendToURLString, , queryString);
return (url, filePath, context);
}
public virtual void ReleaseHandler(IHttpHandler handler)
{
}
}
}
This class should be written in a separate project and then compiled into a file. (Note the file name, if written incorrectly, it will not run normally).
4. Completed.
Open IE and enter http://.../ in the address bar.
The viewer sees that it is the address of a static page, but in fact it is accessing the dynamic web page /web/.
How simple it is.
Of course, this is the simplest, so simple that it is "unusable". Because it will "rewrite" all *.zhtml accesses to /web/ .
As for what kind of web page to rewrite to which web page, I won’t introduce it here (only discuss the methods here, not the details of implementation).
There are many methods. The original work is matched through regularity. I judged it by string sendToUrl = url;.
The rest depends on your needs.