[英]ASP.Net C# Routing; HttpHandler; and HttpModule not working
我在自定義擴展和攔截現有處理程序時遇到了很多問題。
我想做什么
基於持久性選項,我希望所有“虛擬”擴展都由集合處理程序處理。 所有頁面都是動態構建的,站點上沒有實際的文件。 該網站填充內容,形成html輸出並將其作為Web結果返回。 這是必需的,因為我正在兩台服務器之間建立胖/瘦關系。 瘦服務器將簡單地將請求傳遞到胖服務器-胖服務器在該服務器上處理請求,並在線路下發回響應。 該項目適用於動態的多域內容管理系統。 瘦服務器可能不兼容.net(因此需要外部請求),但是將優化.net(因此需要處理程序)。
問題
我想要的是重新路由現有擴展-aspx; php; html。 我已經在我的本地環境中使用設置適當處理程序的自定義HttpModule實現了此目的。 我已經探索了在config中設置標簽的方法,但是擴展使用持久化的動態規則進行了重新路由。
如前所述,該解決方案適用於localhost。 上傳后,.Net擴展名將由模塊正確處理,但是任何自定義擴展名或非.net擴展名均會返回404錯誤。
尋求替代方法,我已經嘗試在Global內進行路由,但這也不起作用。
我還嘗試過使用注冊自定義擴展名...但是每個都遇到了相同的結果-找不到404。
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("{action}.sqs", new SqlRequestHandler()));
}
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime requestValidationMode="2.0" />
<customErrors mode="Off"/>
<httpHandlers>
<add path="*.sqs" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" />
<add path="*.sql" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" />
</httpHandlers>
<httpModules>
<add name="SisBerCMS" type="CmsMapper.VirtualModule, CmsMapper" />
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<modules>
<add name="SisBerCMS" type="CmsMapper.VirtualModule, CmsMapper" />
</modules>
<handlers>
<add path="*.sqs" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" name="sqsHandler" />
<add path="*.sql" verb="*" type="CmsMapper.VirtualHandler, CmsMapper" name="sqlHandler" />
</handlers>
</system.webServer>
if (extentionMap != null)
{
// note that extentionMap.ExtentionType is a predetermined enum
switch (extentionMap.ExtentionType)
{
// If the extention is banned then pass back a generic message
case ExtentionType.Banned:
this.WriteTextResponce("Invalid extention detected:" + extentionMap.Extention);
break;
// Remap .Ajax requests to the ajax handler
case ExtentionType.Ajax:
this._app.Context.RemapHandler(new AjaxHandler());
break;
// Remap session query or sql requests to the sql handler
case ExtentionType.SessionQuery:
this._app.Context.RemapHandler(new SqlRequestHandler());
break;
// if the extention is not ignored, re map to the virtual page handler
default:
bool isManagementServer = this._app.Context.Request.Url.Authority != VirtualModule.RESPONSE_SERVER;
bool isPostRequest = !String.IsNullOrEmpty(this._app.Context.Request.Form[HtmlRequest.RequestOrigin]);
bool isGetRequest = !String.IsNullOrEmpty(this._app.Context.Request.QueryString[HtmlRequest.RequestOrigin]);
bool isIgnored = extentionMap.ExtentionType == ExtentionType.Ignore;
if ((isPostRequest || isGetRequest) && !isIgnored)
{
this._app.Context.RemapHandler(new VirtualHandler());
}
else
{
this._app.Context.RemapHandler(new ExternalRequestHandler());
}
break;
}
}
所有處理程序都是標准的,實現以下內容:
public class SqlRequestHandler : IHttpHandler, IRequiresSessionState, IRouteHandler
再次,首選方法HttpModule在我的本地主機計算機上工作。 這可能是服務器配置問題(在這種情況下,我正在尋找解決方法),但是正在處理.net擴展名的事實很奇怪-因為這意味着中等信任級別的問題不應適用,但是問題關於服務器上擴展處理的信息可能要優先於.net應用程序。
服務器是共享主機(因此我無法更改machine.config文件),是使用4.0的IIS6。
感謝您對如何解決此問題的任何建議。 麥克風
您需要在IIS 6.0中配置網站,以將所有擴展名(包括無擴展名的路徑,稱為通配符擴展名映射 )路由到ASP.NET ISAPI dll(並禁用文件是否存在檢查)。
當然,您只能為要通過ASP.NET代碼路由的那些擴展有選擇地進行此映射。 但是,如果您沒有預定義的擴展集,通配符映射將更加有用。
在沒有此類映射的情況下,IIS不會將對未知擴展名的請求轉發到ASP.NET(並且路由代碼甚至都不會出現)-而是IIS會將擴展名傳遞給默認(靜態文件)處理程序,如果文件存在,則該處理程序將發出404不存在。
請參閱描述這些步驟的文章(適用於ASP.NET MVC,但適用於您的情況): http : //haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6- walkthrough.aspx
在文章結尾處,作者給出了如何添加通配符腳本映射
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.