問題:
公司項目中要求在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 ,則如所示