.net Framework 4.0 url重寫

來源:互聯網
上載者:User
  1.添加Global.asax<br />   2.註冊重寫方法 
   void Application_Start(object sender, EventArgs e)     {        // 在應用程式啟動時啟動並執行代碼        //註冊url重寫        RegisterRoutes(System.Web.Routing.RouteTable.Routes);    }

3.註冊用的方法

    /// <summary>    /// URL重寫註冊類    /// </summary>    /// <param name="routes"></param>   public  void RegisterRoutes(System.Web.Routing.RouteCollection routes)    {               routes.MapPageRoute(            //路由名稱,取一個和當前路由設定相匹配的名稱即可。            "productdetail",            //URL的重寫規則。             //category 為 Request["category"]            //name 為在目標頁面中的 Request["name"]            "products/{category}/{name}.html",            //配置對應的物理檔案。            "~/45newpart/UrlReWriteURL重寫.aspx", false,            //配置預設值            new System.Web.Routing.RouteValueDictionary { { "category", "book" }, { "name", "aspnet" } }            ,            //參數的規則            new System.Web.Routing.RouteValueDictionary() {            { "name", @"\d{1,}" }            }            );        routes.MapPageRoute(            //路由名稱,取一個和當前路由設定相匹配的名稱即可。             "xiaohe",            //URL的重寫規則。             //category 為 Request["category"]            //name 為在目標頁面中的 Request["name"]             "Books/{category}/{name}.html",            //配置對應的物理檔案。             "~/45newpart/UrlReWriteURL重寫.aspx", false,            //配置預設值             new System.Web.Routing.RouteValueDictionary { { "category", "book" }, { "name", "aspnet" } }             ,            //參數的規則             new System.Web.Routing.RouteValueDictionary() {            { "name", @"[\w\W]{1,}" }            }             );    }

4.頁面使用

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UrlReWriteURL重寫.aspx.cs" Inherits="_45newpart_UrlReWriteURL重寫" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title>URL重寫分頁</title>    <link type="text/css" rel="stylesheet" href="http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/default.css" /></head><body>    <form id="form1" runat="server">    <div>       <asp:Label  runat="server" Text=" <%$RouteValue:name %>">" >           </asp:Label>    狼來了的故事告訴我們,被人騙了兩次後,第三次一定要相信他        1.添加Global.asax<br />        2.在        <pre class="csharp" name="code">    void Application_Start(object sender, EventArgs e)     {        // 在應用程式啟動時啟動並執行代碼        //註冊url重寫        RegisterRoutes(System.Web.Routing.RouteTable.Routes);    }            </pre>        中加入 RegisterRoutes(System.Web.Routing.RouteTable.Routes);        <br />        3. 編寫URL重寫類<br />        <pre>     /// <summary>    /// URL重寫註冊類    /// </summary>    /// <param name="routes"></<param>   public  void RegisterRoutes(System.Web.Routing.RouteCollection routes)    {        routes.MapPageRoute(            //路由名稱,取一個和當前路由設定相匹配的名稱即可。            "productdetail",            //URL的重寫規則。             //category 為 Request["category"]            //name 為在目標頁面中的 Request["name"]            "products/{category}/{name}",            //配置對應的物理檔案。            "~/xzz.aspx", false,            //配置預設值            new System.Web.Routing.RouteValueDictionary { { "category", "book" }, { "name", "aspnet" } });    }        </pre>    </div>    </form></body></html>

5.頁面後台代碼

 

 

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _45newpart_UrlReWriteURL重寫 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        string 擷取參數 = RouteData.Values["category"].ToString();        string 擷取參數2 = RouteData.Values["name"].ToString();                Response.Write(擷取參數+"/"+擷取參數2);    }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.