欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net2.0 URL重写以及urlMappings问题(1)】,下面是详细的分享!
asp.net2.0 URL重写以及urlMappings问题(1)
using System;
using System.Web;
public class ReWriteModule : IHttpModule
{
public ReWriteModule()
{
}
public override string ToString()
{
return this.GetType().ToString();
}
void IHttpModule.Dispose()
{
}
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;
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest +=delegate(object sender, EventArgs e)
{
System.Web.HttpContext cxt=context.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);
cxt.Response.Write("");
cxt.Response.Write("转向的目的URL:" + newUrl);
cxt.Response.Write("");
cxt.RewritePath(newUrl);
}
//如果要求处理所有的请求时用到
//else
//{
// cxt.Response.Write(cxt.Request.Path + "");
// cxt.Response.Write("你请求的资源不存在或无权访问!");
// cxt.Response.Flush();
// cxt.Response.End();
//}
}
};
}
}
以上所分享的是关于asp.net2.0 URL重写以及urlMappings问题(1),下面是编辑为你推荐的有价值的用户互动:
相关问题:asp.net如何进行url重写
答:解决方法:如下, 我们的网页URL后经常会跟有多个的参数,比如Information.aspx?ID=1&Type=2,这样的URL让人太直观的看到了参数,感觉不是很好,而且会导致URL很长,所以可以用下面的方法来重写URL,即可以缩短URL、又易于被搜索引擎收录,还可以... >>详细
相关问题:在ASP.NET中如何实现和利用URL重写
答:关于它的好处,我将在本篇文章的“如何利用URL重写”部分来详细的说明。URL重写可以通过编程的方式来实现。ASP.NET中的Contex.RewritePath()方法可以让你从程序中实现重写请求的URL。一旦重写后,系统将使用新的路径来继续执行这个请求。在Global.... >>详细
相关问题:ASP.NET URL重写。
答:可以利用RewritePath重写Url,在程序中就能实现,不需要服务器设置。 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
