Asp.net实现URL重写
【概述】
URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程。重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力;而且在你改变了自己的网站结构后,无需要求用户修改他们的书签,无需其他网站修改它们的友情链接;它还可以提高你的网站的安全性;而且通常会让你的网站更加便于使用和更专业。


class MyUrlWriter : IHttpModule
{
public void Init( HttpApplication context)
{
context.BeginRequest += new EventHandler (context_BeginRequest);
} protected void context_BeginRequest( object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication ;
HttpContext context = application.Context; //上下文
string url = context.Request.Url.LocalPath; //获得请求URL Regex articleRegex = new Regex ("/Article/[A-Z0-9a-z_]+" ); //定义规则
if (articleRegex.IsMatch(url))
{
string paramStr = url.Substring(url.LastIndexOf('/' ) + );
context.RewritePath( "/Article.aspx?id=" + paramStr);
}
else
{
context.RewritePath( "/Default.aspx" );
}
} public void Dispose() { }
}
<httpModules >
<add name = "UrlReWriter " type =" UrlReWriter.MyUrlWriter,UrlReWriter " />
</httpModules >
public partial class Article : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
Response.Write(Request.QueryString[ "id" ]);
}
}
<a href ="/Article/35">测试url重写</a>



/// <summary>
/// Provides a rewriting HttpModule.
/// </summary>
public class ModuleRewriter : BaseModuleRewriter
{
/// <summary>
/// This method is called during the module's BeginRequest event.
/// </summary>
/// <param name="requestedRawUrl"> The RawUrl being requested (includes path and querystring).</param>
/// <param name="app"> The HttpApplication instance. </param>
protected override void Rewrite( string requestedPath, System.Web.HttpApplication app)
{
// log information to the Trace object.
app.Context.Trace.Write( "ModuleRewriter" , "Entering ModuleRewriter"); // get the configuration rules
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules; // iterate through each rule...
for (int i = ; i < rules.Count; i++)
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$" ; // Create a regex (note that IgnoreCase is set...)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); // See if a match is found
if (re.IsMatch(requestedPath))
{
// match found - do any replacement needed
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,re.Replace(requestedPath, rules[i].SendTo)); // log rewriting information to the Trace object
app.Context.Trace.Write( "ModuleRewriter" , "Rewriting URL to " + sendToUrl); // Rewrite the URL
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
break ; // exit the for loop
}
} // Log information to the Trace object
app.Context.Trace.Write( "ModuleRewriter" , "Exiting ModuleRewriter");
}
}
public partial class Article : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
Response.Write(Request.QueryString[ "id" ]);
}
}
public partial class News : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
Response.Write( string .Format("日期:{0}<br/>" , Request.QueryString["date" ]));
Response.Write( string .Format("ID:{0}<br/>" , Request.QueryString["id" ]));
}
}
<configuration>
<configSections>
<section name = "RewriterConfig " type =" URLRewriter.Config.RewriterConfigSerializerSectionHandler,URLRewriter " />
</configSections>
</configuration>
<httpHandlers>
<remove verb = "* " path = "*.asmx " />
<add verb = "* " path = "* " type = "URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb = "* " path = "*.html " type =" URLRewriter.RewriterFactoryHandler, URLRewriter " />
</httpHandlers>
<configuration>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/Article/([A-Z0-9a-z_]+) </LookFor>
<SendTo>~/Article.aspx?id=$1 </SendTo>
< RewriterRule>
<RewriterRule>
<LookFor>~/News/(\d{4}-\d{2}-\d{2})/(\d{1,6})\.html? </LookFor>
<SendTo>~/News.aspx?date=$1 & id=$2</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
</configuration>
<a href ="<% = ResolveUrl("Article/35")%> "> 测试文章url重写</a>< br />
<a href ="<% = ResolveUrl("News/2014-06-11/285864.html")%> "> 测试新闻url重写</a>



Asp.net实现URL重写的更多相关文章
- ASP.net的url重写
http://blog.csdn.net/windok2004/article/details/2432691 1. 有关于URL的重写,本文也只是拿来主意.相继有MS的组件“URLRewriter” ...
- ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL
ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL 引言--- 在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流量. ...
- asp.net 页面url重写
不更改情况下,页面路径为index.aspx?id=1,现在输入页面路径index/1时,也能访问到页面,这一过程叫做url重写 ①:在一个类里制定路径重写规则,以下为自定义UrlRewriterFi ...
- (转)ASP.net的url重写
1. 有关于URL的重写,本文也只是拿来主意.相继有MS的组件“URLRewriter”和在Global.asax里的“Application_BeginRequest()”编码方式,以及IIS里的I ...
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase
原文地址:http://www.51csharp.com/MVC/882.html ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL 引言-- 在初级篇中,我们 ...
- ASP.NET MVC URL重写与优化(初级篇)-使用Global路由表定制URL
ASP.NET MVC URL重写与优化(初级篇)-使用Global路由表定制URL 引言--- 在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流 ...
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
http://www.cnblogs.com/John-Connor/archive/2012/05/03/2478821.html 引言-- 在初级篇中,我们介绍了如何利用基于ASP.NET MVC ...
- [转载]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
引言-- 在初级篇中,我们介绍了如何利用基于ASP.NET MVC的Web程序中的Global文件来简单的重写路由.也介绍了它本身的局限性-依赖于路由信息中的键值对: 如果键值对中没有的值,我们无法将 ...
- 【转】Asp.net实现URL重写
[概述] URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程.重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力:而且在你改变了 ...
随机推荐
- 1.网络工具:ifconfig,ping,netstate,Redhat命令和图形化设置ip,finger,nslookup
1 ip ad查看网卡编号 2.ifconfig查看网卡信息 3.关闭网卡 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcX ...
- 微软研究院的分布式云计算框架orleans
orleans Orleans 客户端请求的消息流转以及消息在Silo中再路由机制 Witte 2015-04-29 21:58 阅读:196 评论:0 一种基于Orleans的分布式Id ...
- Java经典23结构模型的设计模式(三)------附加代理模式、适配器型号、Facade模式的差异
本文介绍了7样的结构模型中的其余2种:轻量级.代理模式. 一.享元模式FlyWeight 享元模式比較简单且重要,在非常多场合都被用到.仅仅只是封装起来了用户看不到.其概念:运用共享内存技术最大限度的 ...
- 第十七章——配置SQLServer(1)——为SQLServer配置更多的处理器
原文:第十七章--配置SQLServer(1)--为SQLServer配置更多的处理器 前言: SQLServer提供了一个系统存储过程,SP_Configure,可以帮助你管理实例级别的配置.微软建 ...
- jquery php 百度搜索框智能提示效果
这个程序是利用php+ajax+jquery 实现的一个仿baidu智能提示的效果,有须要的朋友能够下载測试哦. 代码例如以下 index.html文件,保保存成index.htm <!DOCT ...
- 职业选择測试(A/B卷)
不同性格的人适合从事不同的职业.职业选择对于每一个人都是很重要的事情.假设能选一个既可以发挥潜能又有兴趣的工作,会使整个团队的效率逐倍增长.想了解你更适合什么职业吗?一起来測试一下吧.本套測试分为A卷 ...
- HDU 1176 免费馅饼(DP)
职务地址:HDU 1176 以时间为横轴.11个点位纵轴构造一个矩阵.然后利用数字三角形的方法从上往下递推下去. 代码例如以下: #include <iostream> #include ...
- Java使用串行编程操作继电器
首先,我们必须建立一个良好的环境,那是,jdk并且tomcat.如果它不必须是web装了! 还有就是配置,也就是默认的comm.jar ,javax.comm.properties , win32co ...
- android做设计的每一个屏幕尺寸和分辨率(一个)
一个.与分辨率无关 1.使用dp(dpi) Android密度不依赖像素(dp)指定屏幕尺寸,它同意不同的屏幕尺寸和像素密度类似设备通过缩放来达到同样的效果. (不解决不同屏幕尺寸的问题?) 2.的资 ...
- Log4NET 数据库
阅读目录 Log4NET简介 前提 详细步骤 回到顶部 Log4NET简介 log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种 ...