asp.net URL rewrite simplified version learn URL rewrite

  • 2020-05-07 19:32:32
  • OfStack

One of the easiest ways to implement URL rewrite (URLRewriter) in asp.net.
Reference (author Scott Mitchell translation: Janssen) of the masterpiece, although not fully understand, but also copy to make a tiger, quite "achievement" feeling. Write it down and share 1.
There are a lot of principles in the original, but I won't go into them here. I'll just write the operation here. The goal is to implement one of the simplest programs that can achieve URL rewrite.
1. You need to set 1 of the 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> 


Where *.zhtml is the extension of the page written in the address bar, which is for the user to see, this can be changed at will (but to comply with the rules of the extension!). . Of course, it has to correspond to the Settings in step 1.
3. Write a class.

code
 
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 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 ASP.NET PageHandlerFactory delegates 
/// to.</returns> 
public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) 
{ 
string sendToUrl = url; // The address in the address bar  
string filePath = pathTranslated; 
string sendToURLString = "/web/index.aspx"; // The actual page to visit  
string queryString = ""; // Parameters. Such as  ?id=123 
filePath = context.Server.MapPath(sendToURLString); // Physical address  
// This is the most important sentence. Turn to.  
context.RewritePath(sendToURLString, String.Empty, queryString); 
return PageParser.GetCompiledPageInstance(url, filePath, context); 
} 
public virtual void ReleaseHandler(IHttpHandler handler) 
{ 
} 
} 
} 


This class will be written in a separate project and compiled into ZDILURLRewriter.DLL. Note the file name, which will not work if you write it wrong.
4. Done.
Open IE and type http://... / 1. zhtml.
The viewer sees the address of a static page, but is actually accessing the dynamic page /web/ index.aspx.
How simple.
Of course, this one is the simplest, so simple that it doesn't work. Because he would "rewrite" all *.zhtml access to /web/ index.aspx.
As for what page to rewrite to which page, I won't cover it here (I'll just cover the method, not the implementation details).
There are many methods. The original text is matched by regularization. I used string sendToUrl = url; To judge.
The rest depends on your needs.

Related articles: