一、起因
最近一個項目要實現偽靜態,之前沒接觸過,故一切從零開始,開始網上查資料,方法大概有三種,但是我都試了好幾個,都失敗了。望有建議的博友給點建議,這里我實現了一種方式,是通過微軟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也是一樣的,如下
<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.問題
每個人的電腦設置不同,可能會有各種不同的問題,但去百度基本能找到解決辦法。本人對偽靜態也是一知半解,大多也是查資料得來的信息,如有不同見解,望告知,謝謝。
