All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#动态生成树型结构的Web程序设计实例_.net资料_编程技术
ASP.NET,2.0中客户端脚本使用总结_.net资料_编程技术
visual c++中的构造函数与析构函数
visit c++中Connection与Recordset
小例子:ASP.NET定制简单的错误处理页面_.net资料_编程技术
Sql,server,2005的XML最佳实施策略(2)_[SQL,Server教程]
Visual,Studio,2008,简体中文版下载_[Asp.Net教程]
PHP的十个高级技巧(中)_[PHP教程]
C#中的throw语句使用方法
用PHP简易实现中文分词_[PHP教程]
Reading number is top 10 pictures
The money of more than 100 countries and regions18
Absolutely shocked. National geographic 50 animal photographys9
Ashlynn Brooke a group sexy photo4
美丽的桂林风光1
牛奶和人奶哪个好?
A man's favorite things10
Startling Russian girl blind date scene1
Soong ching ling's former residence1
HongMenYan premiere XinLiangGong clairvoyant outfit PK YiFeiLiu1
The beginning and end
Download software ranking
Boxer Classic video2
matrix1
dreamweaver8中文版
Eclipse-CALMSANNY (second edition)
Boxer's Top ten classic battle7
Popkart Cracked versions Mobile phone games
Proficient in Eclipse
Rio big adventure
Boxer vs Yellow1
VC++6.0培训教程
归海一刀 published in(发表于) 2014/1/30 1:35:22 Edit(编辑)
巧用Asp.net代码在虚拟主机实现Urlrewrite_[Asp.Net教程]

巧用Asp.net代码在虚拟主机实现Urlrewrite_[Asp.Net教程]

巧用Asp.net代码在虚拟主机实现Urlrewrite_[Asp.Net教程]
在网上看到,很多朋友在asp.net中做urlrewrite,用的是HttpHandle+Server.Transfer的方法。其实这种方法是错误的。
第一,HttpHandle是实现不了urlrewrite的;
第二Server.Transfer是标准的重定向,根本不是urlrewrite。
其实,实现urlrewrite不用自己HttpHandle,也不用自己实现HttpModule,用几行代码就可以轻松实现。
我这里介绍的是在虚拟主机上,虚拟主机不同于自己的服务器,你是没有权限去修改IIS,也没有权限去安装iis rewrite之类的IIS插件。但是我们仍然可以轻松完成需要的功能。
具体做法如下:打开global.asax.cs,定位到protected void Application_BeginRequest(Object sender, EventArgs e)。从方法名我想也能猜到它是做什么的。输入如下代码:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
string oldUrl = HttpContext.Current.Request.RawUrl ;

string pattern = @"^(.+)default/(\d+)\.aspx(\?.*)*$";
string replace = "$1default.aspx?id=$2";

if(Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
{
string newUrl = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled |
RegexOptions.IgnoreCase);
this.Context.RewritePath(newUrl);
}
}


有了上边这段代码,我访问一个类似:.../default/123.aspx 的网址,当然这个网址在我的电脑上不存在,它就会被定向到:.../default.aspx?id=123。
当然,利用功能强大的正则表达式,你可以任意按照自己的需要来重写url,这一切都是在服务器端默默的进行,在客户端是不会有任何察觉的。由于是在虚拟主机上,我们只能重定向.aspx文件,如果是自己的服务器,只要把后缀名在IIS中注册一下,就可以实现任意后缀名的处理。比如你可以注册一个*.myweb这样的类型,这样别人访问default/456.myweb时,你可以把它重定向到default.aspx?id=456。总之一句话,只要你能想到.net就可以帮你实现,并且这一切不需要多少的代码。


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.