我有一个处理程序,我想处理所有流量,包括文件等。
但是只要URL匹配物理文件的位置,例如“someFile / test.cshtml”,它就会忽略我的处理程序和BeginProcessRequest,在这种情况下,甚至会以某种方式使用RazorEngine呈现cshtml?
但是,如何防止此行为并确保我的处理程序处理所有请求?
这是我的整个web.Config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpHandlers>
<clear />
<add verb="*" type="SimpleWebServer.HttpHandler" path="*"/>
</httpHandlers>
</system.web>
<system.webServer>
<handlers>
<clear />
<add name="CatchAll" verb="*" type="SimpleWebServer.HttpHandler" path="*" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>
我的Http Handler:
namespace SimpleWebServer
{
public class HttpHandler : IHttpAsyncHandler
{
...
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callback, Object extraData)
{
return AsyncResult.GetAsyncResult(context, callback);
}
...
}
}
答案 0 :(得分:1)
使用HttpModule而不是HttpHandler。模块在管道中更早执行。因此,您无需与主机IIS中的现有处理程序竞争。
HTTP模块
namespace SimpleWebServer
{
public class CustomHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest +=
this.BeginRequest;
context.EndRequest +=
this.EndRequest;
}
private void BeginRequest(Object source,
EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
// do something
}
private void EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
// do something
}
public void Dispose()
{
}
}
}
的Web.Config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="CatchAll" type="SimpleWebServer.CustomHttpModule"/>
</modules>
</system.webServer>
</configuration>