.NET ASP.NET 僞靜態

僞靜態配置

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&amp;key2=$3" /><!--兩個參數-->
      <add virtualUrl="([^-]+)-([^-]*)-([^-]*)-([^-]*).html" destinationUrl="$1.aspx?key1=$2&amp;key2=$3&amp;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:執行結果如圖:

沒有參數空白頁面

一個參數:

倆參數:

 

發佈了180 篇原創文章 · 獲贊 85 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章