简体   繁体   中英

Handler in system.webServer in web.Config is ignored if a file exist at the path specified

I have a handler that I want to handle all traffic, including files etc.

But as soon as the URL matches the location of a physical file, such as "someFile/test.cshtml" it ignores my handler and the BeginProcessRequest and in this case, somehow even renders the cshtml using the RazorEngine?

But how can I prevent this behavior and ensure that all requests gets processed by my handler?

Here is my entire 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>

And my Http Handler:

namespace SimpleWebServer
{
    public class HttpHandler : IHttpAsyncHandler
    {
        ...
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callback, Object extraData)
        {
            return AsyncResult.GetAsyncResult(context, callback);
        }
        ...
    }
}

Use a HttpModule instead of HttpHandler. Modules execute earlier in the pipeline. Therefore, you don't need to compete with existing handlers in host IIS.

HttpModule

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM