300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > ASP.NET URL重写浅析

ASP.NET URL重写浅析

时间:2019-10-05 03:04:52

相关推荐

ASP.NET URL重写浅析

详细参见: /china/msdn/library/webservices//URLRewriting.mspx

最简单的实现,就是在 Global.asax.cs中 Application_BeginRequest 或者是 Application_AuthenticateRequest 事件处理中,对请求的URL进行判断并进行重写:

protected void Application_BeginRequest(Object sender, EventArgs e)

{

HttpApplication app = (HttpApplication) sender;

string requestedPath = app.Request.Path;

string lookFor = @"^/webapptest/urlrewritetest/department/(\w+)\.aspx$";

string sendTo = "/webapptest/urlrewritetest/webform2.aspx?dept=$1";

Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

if (re.IsMatch(requestedPath))

{

string sendToUrl = re.Replace(requestedPath, sendTo);

app.Context.RewritePath(sendToUrl);

}

}

重写主要利用了 HttpContext.RewritePath 方法。

执行效果是将:

http://localhost/WebAppTest/URLRewriteTest/department/Finance.aspx

重写为:

http://localhost/WebAppTest/URLRewriteTest/WebForm2.aspx?Dept=Marketing

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。