僞靜態配置
1. 引用UrlRewriter.dll組件(bin文件夾)
2.配置web.config
(1)在configuration節點下添加如下代碼(死的):
<configSections>
<section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter"/>
</configSections>
如圖:
(2)配置URL規則(活的,供借鑑):
<CustomConfiguration>
<urls>
<add virtualUrl="([^-]+).html" destinationUrl="$1.aspx" /><!--沒有參數-->
<add virtualUrl="([^-]+)-([^-]*).html" destinationUrl="$1.aspx?key1=$2" /><!--一個參數-->
<add virtualUrl="([^-]+)-([^-]*)-([^-]*).html" destinationUrl="$1.aspx?key1=$2&key2=$3" /><!--兩個參數-->
<add virtualUrl="([^-]+)-([^-]*)-([^-]*)-([^-]*).html" destinationUrl="$1.aspx?key1=$2&key2=$3&key3=$4" /><!--三個參數-->
</urls>
</CustomConfiguration>
如圖:
(3)在system.web下添加如下代碼(死的):
<httpModules>
<add type="URLRewriter.RewriterModule, URLRewriter" name="RewriterModule" />
</httpModules>
如圖:
(4)system.webServer配置文件沒有該節點的話之間添加(死的):
<system.webServer>
<handlers>
<add name="urlrewrite" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv4.0,bitness64"/>
</handlers>
</system.webServer>
例:
3.測試:新建一個ceshi.aspx前臺沒有任何代碼(倆參數的)
ceshi.aspx.cs代碼如下:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Text;
public partial class ceshi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string key1 = Request.QueryString["key1"];
string key2 = Request.QueryString["key2"];
if (!string.IsNullOrEmpty(key1))
{
Response.Write("key1=" + key1);
}
if (!string.IsNullOrEmpty(key2))
{
Response.Write("<br>key2=" + key2);
}
}
}
部署到IIS,默認文檔設置ceshi.aspx:執行結果如圖:
沒有參數空白頁面
一個參數:
倆參數: