![](/img/trans.png)
[英]Errors not being redirected to an Http handler if redirectMode=“ResponseRewrite”
[英]HTTP Handler not being invoked
我有一個自定義的HTTP處理程序,可以正確實現IHttpHandler。 以下是我在webConfig中配置的內容。 如果我理解正確,則此塊應捕獲擴展名為.test的所有請求。
<handlers>
<add name="SampleHandler" verb="*" path="*.test"
type="TestApp.App_Start.CustomHandler, TestApp" />
</handlers>
但是,只有當我將三倍的路徑深度應用於請求URL時,才調用此處理程序。 所有其他請求將404。
例如,當路徑為:
localhost:XXX\some\fake\path\file.test
但不適用於:
localhost:XXX\some\file.test
我正在使用ASP.NET MVC 5,並且懷疑它與路由有關。 我還使用了VS2013中提供的示例項目,因此,除了將處理程序添加到項目中之外,我真的沒有做任何其他事情。
更新:
我確定默認路由會造成干擾。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
即使配置了此路由,有沒有辦法使它正常工作?
路由似乎正在干擾處理程序。 為了允許處理程序接收請求,我需要針對當前RouteCollection調用IgnoreRoute方法,而忽略其中帶有.test的任何路由:
在RouteConfig類中
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.test/");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
現在可以使用了。 有一個更好的方法嗎?
我相信未調用HTTP處理程序的原因是因為您正在IIS 6的Web應用程序中注冊。對於IIS 7及更高版本,請執行以下操作:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="SampleHandler"
type="TestApp.App_Start.CustomHandler, TestApp" />
</httpHandlers>
</system.web>
</configuration>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.