在ASP.NET中解析類似http://abc.com/product?x=1111完全解決方案

來源:互聯網
上載者:User

問題:

公司項目中要求在ASP.NET系統中要能夠對類似 http://www.test.com/Product?id=7788 的格式做解析

 

分析:由於,ASP.NET中預設的檔案是.aspx,所以不能直接達到這種效果。原先,我嘗試用IIS的配置解決這個問題,但失敗,因為若建立一個bbb的頁面,當前台發送bbb?fdafdsa=343時,IIS認為存在檔案名稱為bbb?fdafdsa=343的檔案。因為這種原因,才必須用httpmodule + IIS配置來解決這個問題。主要思想是,修改IIS的file和解析器的mapping,讓所有檔案都通過ASP.NET引擎解析,然後建立一個httpmodule,讓任何訪問前運行這個httpmodule,(注意:extension填寫*,check file exist不要選),這樣就可以了。

 

解決方案:

1.   先建立一個web 應用,為HttpModuleTest.Web

2.   在IIS中單擊該應用的屬性,點擊Virtural directory

 

3.   IIS彈出Application Configuration框,點擊Add

 

4.   IIS彈出Add/Edit Appplication Extension Mapping框,設定,並點擊確定

在Executable中填寫:C:\WINNT\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll

在Extension中填寫:*

不要勾check that file exists

 

5.   在VS.NET的中HttpModuleTest.Web中添加一個新類,如所示,該代碼的作用是顯示完整的raw url 名

using System;

using System.Web;

 

namespace HttpModuleTest.Web

{

       public class MyModule : IHttpModule

       {

              public MyModule()

              {

              }

 

              public void Init(HttpApplication context)

              {

                     context.BeginRequest += new EventHandler(context_BeginRequest);

              }

 

              public void Dispose()

              {

              }

 

              private void context_BeginRequest(object sender, EventArgs e)

              {

                     HttpApplication app = (HttpApplication)sender;

                     app.Response.Write(app.Request.RawUrl);

                     app.Response.End();

              }

       }

}

6.   在web.config中做以下配置:

  <system.web>

              <httpModules>

                     <add name="MyModule" type="HttpModuleTest.Web.MyModule, HttpModuleTest.Web" />

       </httpModules>

</system.web>

7.   編譯系統,在瀏覽器中輸入 http://localhost/httpmoduletest.web/cccccdfadsafdsafdsa?fdsafdsafdsa ,則如所示

 

 

相關文章

聯繫我們

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