asp.net do not use component URL rewriting (for larger projects) _ Practical Tips

Source: Internet
Author: User
First in the site root directory to create a config folder, and then create a urls.config file under this frame, where the configuration information log URL code as follows:
Copy Code code 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 write a rule here, and then modify the site's Web.config file, the modified code is:
Copy Code code as follows:

<?xml version= "1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<authentication mode= "Windows"/>

<add type= "My. Forum.httpmodule "name=" HttpModule "/>

<compilation debug= "true"/>
</system.web>
<!--
Running ASP.net AJAX requires system.webserver under Internet Information Services 7.0
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>

I would have written only one
Copy Code code as follows:


<add type= "My. Forum.httpmodule "name=" HttpModule "/>


On it, I wrote here Two main is to demonstrate how to compatible IIS7, and then under the solution to create a new project (class library), what name does not matter, the main thing is that this project must have a namespace for my. The Forum class file.

The specific code I posted out the detailed explanations I have written in the note.
Copy Code code as follows:

namespaces to use
Using System;
Using System.Diagnostics;
Using System.Threading;
Using System.Web;
Using System.Xml;
Using System.Text.RegularExpressions;
Using System.IO;

Note namespace
Namespace My. Forum
{
Inherit from IHttpModule interface
public class HttpModule:System.Web.IHttpModule
{
<summary>
Init method for implementing an interface
</summary>
<param name= "Context" ></param>
public void Init (HttpApplication context)
{
Set up a delegate for him to perform the following Reurl_beginrequest event
Context. BeginRequest + = new EventHandler (reurl_beginrequest);
}
<summary>
The Dispose method that implements the interface
</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 one of the following classes, where you can refactor
foreach (siteurls.urlrewrite URL in Siteurls.getsiteurls (). Urls)
{
Whether the matching option was found
if (Regex.IsMatch (Requestpath, URL). Pattern, Regexoptions.none | Regexoptions.ignorecase))
{
Start replacing the URL that our program can read
String newurl = Regex.Replace (requestpath.substring (context). Request.Path.LastIndexOf ("/")),
Url. Pattern, url. QueryString, Regexoptions.none | Regexoptions.ignorecase);
Here you can output a look at
Context. Response.Write (URL. Page + "<br>" + newurl+ "<br>");
Begin to replace the user's memorable URL with a URL that the program can read
Context. RewritePath (URL. Page, String. Empty, Newurl);
}
}
}
}
public class Siteurls
{
Defined as Volatitle type is mainly for the convenience of multiple threads access, in this sample program is not practical, the project is big and useful
Private static volatile siteurls instance = null;
String urlsfile = HttpContext.Current.Server.MapPath ("/config/urls.config");
Define two properties
Private System.Collections.ArrayList _urls;
Public System.Collections.ArrayList Urls
{
get {return _urls;}
set {_urls = value;}
}
This is a key implant. Fear of the table is not advanced.
Private System.Collections.Specialized.NameValueCollection _paths;
Public System.Collections.Specialized.NameValueCollection Paths
{
get {return _paths;}
set {_paths = value;}
}
Constructors
Private Siteurls ()
{
Urls = new System.Collections.ArrayList ();
Paths = new System.Collections.Specialized.NameValueCollection ();
Read the config file as an XML number
XmlDocument urlconfig = new XmlDocument ();
Urlconfig. Load (Urlsfile);
XmlNode root = urlconfig. selectSingleNode ("URLs");
foreach (XmlNode n in Root). ChildNodes)
{
Xmlnodetype.comment if it's 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);
It's all in the URL entity class
Urls.add (name, New Urlrewrite. Value, pattern. Value, page. Value.replace ("^", "&"),
QueryString. Value.replace ("^", "&"));
}
}
}
}
Instantiating with methods
public static Siteurls Getsiteurls ()
{
if (instance = null)
{
Instance = new Siteurls ();
}
return instance;
}

#region URL entity class
public class Urlrewrite
{
#region member Variable
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 Constructors
Public Urlrewrite (string name, string pattern, String page, String querystring)
{
_name = Name;
_pattern = pattern;
_page = Page;
_querystring = querystring;
}
#endregion
}
#endregion
}
}

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.