ASP.NET路由應用及IIS配置(非MVC)

來源:互聯網
上載者:User

標籤:c   tar   ext   a   檔案   使用   

一、前後台代碼:

Global.cs:

using System.Web.Routing;

。。。      

void Application_Start(object sender, EventArgs e)

        {

            // 在應用程式啟動時啟動並執行代碼

            //RegisterRoutes();

            RegisterRoutes(RouteTable.Routes);

        }

        public static void RegisterRoutes(RouteCollection routes)

        {

            routes.MapPageRoute("ProductsRoute", "Products/list-{lbid}", "~/Products/ProList.aspx");

            routes.MapPageRoute("ProductsDetail", "Products/show-{id}.html", "~/Products/Show.aspx");

        }

 

 

 

 

Default.aspx添加連結:

<p>

    <ul>

      <li><a href="#">產品中心</a></li>

      <li>·<a href="Products/list-1">產品分類1</a></li>

      <li>·<a href="Products/list-2">產品分類2</a></li>

    </ul>

</p>

<p>

    <ul>

      <li><a href="Products/show-1.html">產品1</a></li>

      <li>·<a href="Products/show-2.html">產品2</a></li>

      <li>·<a href="Products/show-3.html">產品3</a></li>

    </ul>

</p>

 

ProList.aspx.cs 中接收路由參數:

protected void Page_Load(object sender, EventArgs e)

        {

            string lbid = Page.RouteData.Values["lbid"] as string;//接收路由參數

            lblLbid.Text = "lbid="+lbid;

            //下面寫商務邏輯

        }

 

 

 

Products/Show.aspx 接收路由參數:

 

 

protected void Page_Load(object sender, EventArgs e)

        {

            lblMsg.Text = "id=" + Page.RouteData.Values["id"] as string;//接收路由參數;

        }  

 效果

 

 

二、IIS部署

如果訪問html時出現 404 錯誤,有以下兩種解決方案:

1. 在IIS7.0中,需要把網站應用程式程式池中的託管管道模式設定為:整合。否則會提示 404 錯誤。

?

2. 如果實際條件不允許設定為整合模式,則需要添加“處理應用程式對應”,需添加.html檔案,使用asp.net api 處理。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.