您的位置:首页 > 编程语言 > ASP

ASP.NET 如何实现伪静态 url重写路由功能

2012-08-22 16:19 1241 查看
2010-12-04 13:54



‍ ASP.NET 如何实现伪静态

其实所谓的伪静态页面,就是指的URL重写.

1.首先在web.config里写

view plaincopy to clipboardprint?

<configSections>

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>

</configSections>

<configSections>

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>

</configSections>

2.在web.config里添加以下节点

view plaincopy to clipboardprint?

<httpHandlers>

<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

</httpHandlers>

<httpHandlers>

<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

</httpHandlers>

3.配置重写URL规则 (这里我们就以 *.html转到*.aspx为例子,当然也可以实现 http://www.a.com/a-1.html 转到 http://www.a.com/a.aspx?id=1 这种形式),

在configuration 加入一下节点

view plaincopy to clipboardprint?

<RewriterConfig>

<Rules>

<RewriterRule>

<LookFor>~/(.*).html</LookFor>

<SendTo>~/$1.aspx</SendTo>

</RewriterRule>

</Rules>

</RewriterConfig>

<RewriterConfig>

<Rules>

<RewriterRule>

<LookFor>~/(.*).html</LookFor>

<SendTo>~/$1.aspx</SendTo>

</RewriterRule>

</Rules>

</RewriterConfig>

4.这一步 也是最重要的一步。在iis 中右键项目→属性→主目录→配置→映射→添加

可执行文件里面输入 c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll,

扩展名输入 .html

然后再把 确认文件是否存在 前面的钩钩去掉。(这步很重要,不然会爆404的错误)

如下图所示:

5.您需要在你的项目中引用 URLRewriter.dll 这个dll文件 。

下载地址: http://download.csdn.net/source/2325865

6.这样 就配置完了,假设 你有 http://192.168.0.2/index.aspx 这个页面的话。

那么你在浏览器中输入 http://192.168.0.2/index.html 就看到效果了.

注:以上 是在 server2003 iis 6.0 framework2.0 下配置的。

附上 完整的Web.Config代码吧

view plaincopy to clipboardprint?

<?xml version="1.0"?>

<configuration>

<!--1-->

<configSections>

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>

</configSections>

<appSettings/>

<connectionStrings/>

<system.web>

<compilation debug="true"/>

<authentication mode="Windows"/>

<!--2-->

<httpHandlers>

<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

</httpHandlers>

</system.web>

<!--3-->

<RewriterConfig>

<Rules>

<RewriterRule>

<LookFor>~/(.*).html</LookFor>

<SendTo>~/$1.aspx</SendTo>

</RewriterRule>

</Rules>

</RewriterConfig>

</configuration>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhou5157/archive/2010/05/07/5567274.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐