简体   繁体   English

如果指定路径上存在文件,则会忽略web.Config中system.webServer中的处理程序

[英]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? 但是,只要URL匹配物理文件的位置(例如“ someFile / test.cshtml”),它就会忽略我的处理程序和BeginProcessRequest,在这种情况下,甚至可以使用RazorEngine渲染cshtml吗?

But how can I prevent this behavior and ensure that all requests gets processed by my handler? 但是,如何防止这种行为并确保我的处理程序处理所有请求?

Here is my entire web.Config 这是我的整个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: 还有我的Http处理程序:

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. 使用HttpModule而不是HttpHandler。 Modules execute earlier in the pipeline. 模块在管道中更早执行。 Therefore, you don't need to compete with existing handlers in host IIS. 因此,您不需要与主机IIS中的现有处理程序竞争。

HttpModule 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 web.config system.webServer与system.web - web.config system.webServer Vs system.web 重写如何在web.config system.webserver中工作 - How does the rewrite works in web.config system.webserver 如何以编程方式更改web.config中的system.webServer / defaultDocument - How to change system.webServer/defaultDocument in web.config programmatically system.webserver中的设置有时会从web.config文件中删除 - Settings from system.webserver sometimes removed from web.config file 如何从web.config system.web和system.webServer读取Ihttpmodule - how to Read Ihttpmodule from web.config system.web and system.webServer web.config iis7.0集成system.webserver处理程序和模块 - web.config iis7.0 integrated system.webserver handlers and modules web.config 继承:<clear/> 导致 XML 解析错误<system.webServer><modules> - web.config inheritance: <clear/> causes XML Parsing Error in <system.webServer><modules> 需要有关system.webServer重写规则的正则表达式设计的帮助。 - Need help for regex design in system.webServer rewrite rule in web.config 我的IIS 7 web.config中的system.webServer / security / authorization无法正常工作 - The system.webServer/security/authorization in my IIS 7 web.config is not working 用于访问system.webServer的基本IIS 7 web.config在哪里,以及如何通过代码对其进行编辑 - Where is the base IIS 7 web.config for accessing system.webServer and how do you edit it through code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM