First, create a config folder under the root directory of the website, and then create a urls. config file under this folder. The url configuration code recorded here is as follows:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Urls>
<Rewrite name = "default"
Path = "/default-{0}-{1}. aspx"
Pattern = "/default-(\ d +) (-(\ d + ))?. Aspx"
Page = "/default. aspx"
Querystring = "id = $1 ^ page = $3"/>
</Urls>
I only wrote one rule here, and then modified the website's web. config file. The modified code is:Copy codeThe Code is as follows: <? Xml version = "1.0"?>
<Configuration>
<AppSettings/>
<ConnectionStrings/>
<System. web>
<Authentication mode = "Windows"/>
<HttpModules>
<Add type = "my. Forum. HttpModule" name = "HttpModule"/>
</HttpModules>
<Compilation debug = "true"/>
</System. web>
<! --
To run ASP. net ajax in Internet Information Service 7.0, system. webServer is required.
Section. This section is not required for earlier versions of IIS.
-->
<System. webServer>
<Validation validateIntegratedModeConfiguration = "false"/>
<Modules>
<Add type = "my. Forum. HttpModule" name = "HttpModule"/>
</Modules>
</System. webServer>
</Configuration>
Write only oneCopy codeThe Code is as follows: <Add type = "my. Forum. HttpModule" name = "HttpModule"/>
</HttpModules>
Now, I wrote two things here to demonstrate how IIS7 is compatible, and then create a project (Class Library) under the solution. It doesn't matter what the name is, this project must have a namespace named my. forum class file.
I posted detailed explanations for the specific code and wrote them in the comments.Copy codeThe Code is as follows: // The namespace used
Using System;
Using System. Diagnostics;
Using System. Threading;
Using System. Web;
Using System. Xml;
Using System. Text. RegularExpressions;
Using System. IO;
// Note the namespace
Namespace my. Forum
{
// Inherited from the IHttpModule Interface
Public class HttpModule: System. Web. IHttpModule
{
/// <Summary>
/// Implement the Init METHOD OF THE INTERFACE
/// </Summary>
/// <Param name = "context"> </param>
Public void Init (HttpApplication context)
{
// Create a delegate for him to execute the following ReUrl_BeginRequest event
Context. BeginRequest + = new EventHandler (ReUrl_BeginRequest );
}
/// <Summary>
/// Implement the interface's Dispose method
/// </Summary>
Public void Dispose ()
{
}
Private void ReUrl_BeginRequest (object sender, EventArgs e)
{
HttpContext context = (HttpApplication) sender). Context;
String requestPath = context. Request. Path. ToLower ();
// SiteUrls is the following class, which can be reconstructed here
Foreach (SiteUrls. URLRewrite url in SiteUrls. GetSiteUrls (). Urls)
{
// Whether matching options are found
If (Regex. IsMatch (requestPath, url. Pattern, RegexOptions. None | RegexOptions. IgnoreCase ))
{
// Start with the url that our program can understand
String newUrl = Regex. Replace (requestPath. Substring (context. Request. Path. LastIndexOf ("/")),
Url. Pattern, url. QueryString, RegexOptions. None | RegexOptions. IgnoreCase );
// Here you can output it.
// Context. Response. Write (url. Page + "<br>" + newUrl + "<br> ");
// Start to replace the user-friendly URL with the url that the program can understand
Context. RewritePath (url. Page, string. Empty, newUrl );
}
}
}
}
Public class SiteUrls
{
// It is defined as the volatitle type mainly for convenient multi-threaded access. In this example, the program has no practical significance and is useful when the project is large.
Private static volatile SiteUrls instance = null;
String UrlsFile = HttpContext. Current. Server. MapPath ("/config/urls. config ");
// Define two attributes
Private System. Collections. ArrayList _ Urls;
Public System. Collections. ArrayList Urls
{
Get {return _ Urls ;}
Set {_ Urls = value ;}
}
// This is a key-planting fear of the table.
Private System. Collections. Specialized. NameValueCollection _ Paths;
Public System. Collections. Specialized. NameValueCollection Paths
{
Get {return _ Paths ;}
Set {_ Paths = value ;}
}
// Constructor
Private SiteUrls ()
{
Urls = new System. Collections. ArrayList ();
Paths = new System. Collections. Specialized. NameValueCollection ();
// Read the CONFIG file as XML
XmlDocument urlconfig = new XmlDocument ();
Urlconfig. Load (UrlsFile );
XmlNode root = urlconfig. SelectSingleNode ("urls ");
Foreach (XmlNode n in root. ChildNodes)
{
// XmlNodeType. Comment if it is not a Comment
If (n. NodeType! = XmlNodeType. Comment & n. Name. ToLower () = "rewrite ")
{
XmlAttribute name = n. Attributes ["name"];
XmlAttribute path = n. Attributes ["path"];
XmlAttribute page = n. Attributes ["page"];
XmlAttribute querystring = n. Attributes ["querystring"];
XmlAttribute pattern = n. Attributes ["pattern"];
If (name! = Null & path! = Null & page! = Null
& Querystring! = Null & pattern! = Null)
{
Paths. Add (name. Value, path. Value );
// All objects are compressed into the url object class.
Urls. Add (new URLRewrite (name. Value, pattern. Value, page. Value. Replace ("^ ","&"),
Querystring. Value. Replace ("^ ","&")));
}
}
}
}
// Instantiate using methods
Public static SiteUrls GetSiteUrls ()
{
If (instance = null)
{
Instance = new SiteUrls ();
}
Return instance;
}
# Region url entity class
Public class URLRewrite
{
# Region member variables
Private string _ Name;
Public string Name
{
Get {return _ Name ;}
Set {_ Name = value ;}
}
Private string _ Pattern;
Public string Pattern
{
Get {return _ Pattern ;}
Set {_ Pattern = value ;}
}
Private string _ Page;
Public string Page
{
Get {return _ Page ;}
Set {_ Page = value ;}
}
Private string _ QueryString;
Public string QueryString
{
Get {return _ QueryString ;}
Set {_ QueryString = value ;}
}
# Endregion
# Region Constructor
Public URLRewrite (string name, string pattern, string page, string querystring)
{
_ Name = name;
_ Pattern = pattern;
_ Page = page;
_ QueryString = querystring;
}
# Endregion
}
# Endregion
}
}