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是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力:而且在你改变了 ...
随机推荐
- hdu 1298 T9(特里+DFS)
pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...
- NET中小型企业项目开发框架系列(一个)
当时的前端,我们开发了基于Net一组结构sprint.NET+NHibernate+MVC+WCF+EasyUI等中小型企业级系统开发平台,如今把整个开发过程中的步步进展整理出来和大家分享,这个系列可 ...
- ios 多线程开发(一)简介
简介 线程是在一个程序中并发的执行代码的方法之一.虽然有一些新的技术(operations, GCD)提供了更先进高效的并发实现,OS X和iOS同时也提供了创建和维护线程的接口. 这里将要介绍线程相 ...
- [模拟Android微信]主界面
首先看很像模仿: 走出来: 实现过程: 依赖类库:actionbarsherlock 用actionbarsherlock来实现顶部的搜索的效果. tab用的是Viewpaper实现的. 详细细节: ...
- JAVA设计模式(08):结构化-飞锤(Flyweight)
当前咱们国家正在大力倡导构建和谐社会,当中一个非常重要的组成部分就是建设资源节约型社会,"浪费可耻,节俭光荣". 在软件系统中,有时候也会存在资源浪费的情况,比如在计算机内存中存储 ...
- 允许Ubuntu14.04"保存"屏幕亮度值
Ubuntu / Debian 该系统有一个共同的问题,也就是说,每个引导.系统会打开你的屏幕亮度调至最高值. 我很奇怪,为什么14.04这一问题的版本号依然不动. 但是,我们可以做一个脚本Ubunt ...
- CentOS7 防火墙 firewall-cmd
最小化安装 CentOS7 后,很多端口默认是不打开的,需要通过 firewall-cmd 把这些端口打开. 检查防火墙状态 # firewall-cmd --state running 关闭防 ...
- Cocos2d-x 3.1.1 学习日志16--A星算法(A*搜索算法)学问
A *搜索算法称为A星算法.这是一个在图形平面,路径.求出最低通过成本的算法. 经常使用于游戏中的NPC的移动计算,或线上游戏的BOT的移动计算上. 首先:1.在Map地图中任取2个点,開始点和结束点 ...
- Programming from the ground up(0)
这本书的英文版是开源.我读了一些.但是,支持的英语水平不走太,然后还有那些谁译的书,但感觉不是太干脆翻译,在一些地方难以清除作者的思路,所以,我要揍很难理解他自己翻译一下原来的地方,这将更好地了解一点 ...
- 于ubuntu配置hadoop当问题
1. 构造ssh登录 我们并不需要改变/etc/ssh/sshd_config 2. 新hadoop用户,home下列不hadoop夹 创建使用下面的命令 useradd -m hadoop 3. n ...