Problem with URL rewriting in asp.net 1.1

Source: Internet
Author: User
Tags filter constructor httpcontext iis regular expression
Asp.net| problem

1:asp.net1.1 problems in rewriting in the!!!

such as the following regular expression:

<Rules>
<RewriterRule>
<LookFors>
<lookfor>~/(\d{4})/(\d{2}) \.html</lookfor>---------<1>
<lookfor>~/(\d{4})/(\d{2})/</lookfor>--------------<2>
<lookfor>~/(\d{4})/(\d{2}) </LookFor>-----------<3>
<lookfor>~/(\d{4})/(\d{2})/index.html</lookfor>----<4>
</LookFors>
<SendTo>~/Pro.aspx?year=$1&month=$2</SendTo>
</RewriterRule>
</Rules>

1, 4 of which can be mapped to the corresponding page normally

http404 errors can occur 2,3!!!

The reason for this is the process of the IIS itself, and the solution is to rewrite 404 processing errors on the site itself!!!

1: Customize the URL to handle the 404 error (configured in IIS, configured in Web.config to overwrite useless)

2: Add the following section to the system.web section:


<add verb= "*" path= "404.aspx" type= "Lt. Http404,lt "></add>
<add type= "Lt. Rewritemodule,lt "name=" Modulerewriter "/>

The source code is as follows:

public class Http404:System.Web.IHttpHandler
{
Public Http404 ()
{
//
TODO: Add constructor logic here
//
}
#region IHttpHandler Members

public void ProcessRequest (System.Web.HttpContext context)
{
TODO: Add http404.processrequest implementation
String Errorpath=context. Request.RawUrl.Split (New char[]{'; '}) [1];
String Apppath=context. Request.applicationpath;
int Ipos=errorpath.indexof (AppPath);

String url=errorpath.substring (Ipos+apppath.length);
if (!url. EndsWith ("/"))
//   {
url+= "/";
//   }
url+= "index.html";
Context. Response.Write (URL);
Context. RewritePath (URL);

  //context. Response.Write (URL);
      url= "~" +url;
   string Newurl =lt. Rewritemodule.geturl (Context,url);
  //context. Response.Write (Newurl);
   if (newurl!= null)
   {
   //cxt. Response.filter = new Responsefilter (CXT. Response.filter,cxt. Request.path);
    context. Response.Write ("The requested path:" + URL);
    context. Response.Write ("<BR>");
    context. Response.Write ("The purpose of the steering URL:" + newurl);
    context. Response.Write ("<BR>");
    context. RewritePath (Newurl);
  }
   Else
   {
    context. Response.Write ("The resources you requested do not exist!!") ");
    context. Response.End ();
  }
 }

public bool IsReusable
{
Get
{
TODO: Add http404.isreusable Getter Implementation
return false;
}
}

The HttpModule in configuration section processing are as follows:

public class ReWriteModule:System.Web.IHttpModule
{
Public Rewritemodule ()
{
//
TODO: Add constructor logic here
//
}
#region IHttpModule Members

public void Init (System.Web.HttpApplication context)
{
TODO: Add Rewritemodule.init implementation
Context. Beginrequest+=new EventHandler (this. ReWrite);
}
private static System.Xml.XmlDocument Ruledoc = null;
private static System.Xml.XmlDocument Getruleconfig (System.Web.HttpContext app)
{
if (Ruledoc = null)
{
Ruledoc = new System.Xml.XmlDocument ();
Ruledoc.load (app. Server.MapPath ("~/rule.xml"));
}
return ruledoc;
}
public static string GetUrl (System.Web.HttpContext cxt,string path)
{

System.Xml.XmlDocument doc = Getruleconfig (CXT);
System.Xml.XmlNodeList lst= Doc. getElementsByTagName ("Rewriterrule");
String pat= "";
foreach (System.Xml.XmlNode nd in LST)
{
System.Xml.XmlNodeList sub = nd. Childnodes[0]. ChildNodes;
foreach (System.Xml.XmlNode chk in sub)
{
Pat = "^" + chk. innertext+ "$";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex (PAT, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (Reg. IsMatch (PATH))
{
Return Reg. Replace (Path, ND. CHILDNODES[1]. InnerText);
}
}
}
return null;

}
private void ReWrite (Object Sender,eventargs e)
{
System.Web.HttpContext CXT = (sender as System.Web.HttpApplication). context;

if (CXT. Request.contenttype!= "Image/pjpeg")
{
String type = Cxt. Request.ContentType.ToLower ();
String path = Cxt. Request.path;
String AppPath = Cxt. Request.applicationpath;
Path = path. Remove (0, AppPath. Length);
Path = "~" + path;

String newurl = GetUrl (cxt, path. TrimEnd (). TrimStart ());
if (Newurl!= null)
{
Cxt. Response.filter = new Responsefilter (CXT. Response.filter,cxt. Request.path);
Cxt. Response.Write ("Path of the request:" + path);
Cxt. Response.Write ("<BR>");
Cxt. Response.Write ("The purpose of the steering URL:" + newurl);
Cxt. Response.Write ("<BR>");
Cxt. RewritePath (Newurl);



}
Else
//{
Cxt. Response.Write (CXT. Request.path + "<BR>");
Cxt. Response.Write ("The resources you requested do not exist or have no access!") ");
Cxt. Response.Flush ();
Cxt. Response.End ();
//}
}
}
public void Dispose ()
{
TODO: Add Rewritemodule.dispose implementation
}

#endregion
}

The configuration of the---------Rule.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<Rules>
<RewriterRule>
<LookFors>
<lookfor>~/(\d{4})/(\d{2}) \.html</lookfor>
<lookfor>~/(\d{4})/(\d{2})/</lookfor>
<lookfor>~/(\d{4})/(\d{2}) </LookFor>
<lookfor>~/(\d{4})/(\d{2})/index.html</lookfor>
</LookFors>
<SendTo>~/Pro.aspx?year=$1&month=$2</SendTo>
</RewriterRule>
<RewriterRule>
<LookFors>
<lookfor>~/pro.aspx?year= (\d{4}) &month= (\d{2}) </LookFor>
</LookFors>
<sendto>~/(\d{4})/(\d{2}) \.html</sendto>
</RewriterRule>
<RewriterRule>
<LookFors>
<LookFor>~/pc</LookFor>
</LookFors>
<SendTo>~/Test2.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFors>
<LookFor>~/index.html</LookFor>
<LookFor>~/default.html</LookFor>
</LookFors>
<SendTo>~/default.aspx</SendTo>
</RewriterRule>
</Rules>



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.