方法一、
程序要调整的部分只有两块。
一是web.config文件。
二是链接地址。
所需urlrewrite.dll
首先下载URLRewriter:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
下载安装后再bin目录下找到URLRewriter.dll文件
好了开始实施。
第一步:将urlrewrite.dll下载到你的web程序目录里去。哪都行。我是放在bin里面的。然后添加引用,将urlrewrite.dll引用进来。
第二步:修改web.config
这一步要修改几个地方。要注意位置是不同的
1 在web.config文件中加入如下代码,注意要放在<configuration>下面, <appSettings/>
<connectionStrings/> <system.web>上面不然会出错
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
其中
<section name="RewriterConfig"
type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter.dll文件中
2 在web.config文件中的system.web节点下加入如下代码
<httpHandlers>
<add verb="*" path="*.html"
type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*"
type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
这段代码的意思是:将文件扩展名为.html和任意扩展名(包括无扩展名,不包括*.html,因为这个位置在上面会先处理)的文件的所有 HTTP 请求映射到类 URLRewriter.RewriterFactoryHandler,注意顺序,按从上到下执行,如果path="*"在上面的话,则下面的html映射则无效,下面步骤中有映射到那个页面的设置
3 重写urlDE> DE>
<configuration>节点下面
关键就是
<
RewriterConfig
>
<
Rules
>
<
RewriterRule
>
<
LookFor
>
~/(.+).html
</
LookFor
>
<
SendTo
>
~/Shownews.aspx?ShowID=$1
</
SendTo
>
</
RewriterRule
>
<
RewriterRule
>
<
LookFor
>
~/(.+)
</
LookFor
>
<
SendTo
>
~/blog.aspx?UserName=$1
</
SendTo
>
</
RewriterRule
>
</
Rules
>
</
RewriterConfig
>
效果:当访问http://127.0.0.1/123.html时,实际访问的是http://127.0.0.1/Shownews.aspx?ShowID=123
访问http://127.0.0.1/任意字符时,实际访问的是http://127.0.0.1/blog.aspx?UserName=任意字符
注意第2,3步中的映射顺序
其中关键在url的转换
<
LookFor
>
~/(.+).html
</
LookFor
>
<
SendTo
>
~/Shownews.aspx?ShowID=$1
</
SendTo
>
意思是把第一个路径转成另一个路径。其中<LookFor>()中的正则表达式就是第二句中的参数$1 .
同样也可以用$2 $3来表示<LookFor>中第二 第三个()中的参数。
多个参数:
<ReWriterUrls>
<rule>
<old>(.*)/TestUrlRe/file(.*)/(.*)\.html</old>
<new>../WebForm1.aspx?id=$2&type=$3</new>
</rule>
<rule>
<old>(.*)/TestUrlRe/t(.*)/(.*)\.html</old>
<new>../WebForm1.aspx?tid=$2&ttype=$3</new>
</rule>
</ReWriterUrls>
第三步:在页面程序中可以这样写:
<a href="news_<%=newsid%>.html" target="_blank">新闻标题</a>
完成上面三个步骤就可以轻松实现URL重写了,不过需要注意的是:如果发布网站的话,你会发现你的URL重写有可能会失效,如果失效的话就需要您设置一下IIS:
打开IIS,主目录-〉配置-〉映射-〉点击“插入”通配符应用程序映射-〉选择“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll”,然后把勾选去掉(一定要去掉),然后确定。
上面设置完毕之后,就可以正常浏览了。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
方法二
Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
string oldUrl = HttpContext.Current.Request.RawUrl;
string pattern = @"^(.+)show/show-(\w+).html(\?.*)*$";
string replace = "$1admin_GetUserInfo.aspx?key=$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);
}.aspx页面使用
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="768px">
<Columns>
<asp:TemplateField HeaderText="用户名">
<ItemTemplate>
<a href="show/show-<%# myEncrypt(DataBinder.Eval(Container.DataItem,"ID").ToString())%>.html"><%# Eval("User_Name") %></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>/// <summary>
/// 加密
/// </summary>
/// <param name="strid"></param>
/// <returns></returns>
public string myEncrypt(string strid)
{
return HEYCcrypt.Encrypt(strid);
}