一、起因
最近一个项目要实现伪静态,之前没接触过,故一切从零开始,开始网上查资料,方法大概有三种,但是我都试了好几个,都失败了。望有建议的博友给点建议,这里我实现了一种方式,是通过微软URLRewriter.dll实现的,觉得不错,故记录下来,大家一起学习,话不多说,上代码。
效果截图:
二、配置web.config
首先,新建项目。添加一个页面Default.aspx,用于测试。去下一个URLRewriter.dll,链接: https://pan.baidu.com/s/1hs3YNZm 密码: n6jx,添加引用即可;然后,开始配置web.config
1.调用URLRewriter.dll
<configSections> <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections>
2.配置伪静态规则,至于规则的配置,可取google上去查。
<system.webServer> <rewrite> <rules> <rule name="Rule1"> <match url="^123_t([0-9]+)\.html$" ignoreCase="false" /> <action type="Rewrite" url="Default.aspx?id={R:1}" appendQueryString="false" /> </rule> <rule name="Rule2"> <match url="^123.html" ignoreCase="false" /> <action type="Rewrite" url="Default.aspx" appendQueryString="false" /> </rule> <rule name="Rule3"> <match url="^job/l-([0-9]+)-([0-9]+)\.html$" ignoreCase="false" /> <action type="Rewrite" url="JobList.aspx?id={R:1}&page={R:2}" appendQueryString="false" /> </rule> </rules> </rewrite> <validation validateIntegratedModeConfiguration="false" /> </system.webServer>
补充:网上也有另一种配置方法,这里也贴下,引用的dll也是一样的,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<configSections> <section name= "RewriterConfig" type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections> <RewriterConfig> <Rules> <RewriterRule> <LookFor>~/Index.html</LookFor> <SendTo>~/Index.aspx</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/Index/t_([0-9]*)/ID_([0-9]*).html$</LookFor> //如:http://localhost:60948/Index/t_8/ID_456.html <SendTo>~/Index.aspx?type=$1&id=$2</SendTo>s </RewriterRule> </Rules> </RewriterConfig> <system.web> <httpHandlers> <add verb= "*" path= "*.aspx" type= "URLRewriter.RewriterFactoryHandler, URLRewriter" /> <add verb= "*" path= "*.html" type= "URLRewriter.RewriterFactoryHandler, URLRewriter" /> </httpHandlers> <compilation debug= "true" targetFramework= "4.5" /> <httpRuntime targetFramework= "4.5" /> </system.web> <system.webServer> <handlers> <add verb= "*" path= "*.html" type= "URLRewriter.RewriterFactoryHandler,URLRewriter" name= "urlrewriter" /> </handlers> <validation validateIntegratedModeConfiguration= "false" /> </system.webServer> |
3.问题
每个人的电脑设置不同,可能会有各种不同的问题,但去百度基本能找到解决办法。本人对伪静态也是一知半解,大多也是查资料得来的信息,如有不同见解,望告知,谢谢。