【问题标题】:Handler in system.webServer in web.Config is ignored if a file exist at the path specified如果指定路径中存在文件,则忽略 web.Config 中 system.webServer 中的处理程序
【发布时间】:2018-09-19 10:12:44
【问题描述】:

我有一个处理程序,我想处理所有流量,包括文件等。

但只要 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 处理程序:

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

【问题讨论】:

  • 使用失败的请求跟踪来查看处理这些 URL 的处理程序。只有这样你才能看到是否可以找到解决方案。
  • 请求不会失败。我的处理程序没有处理它。但我会检查一下,谢谢!
  • 您已将此标记为 asp.net-mvc,因此假设该项目已安装了它的各个方面。查看您的创业公司或 global.asax。还要检查应用程序是否没有从 IIS 继承配置。
  • @BjarkeCK 失败的请求追踪也可以追踪成功的请求。
  • 您是在经典模式下运行还是在集成模式下运行?

标签: asp.net .net iis iis-express


【解决方案1】:

使用 HttpModule 而不是 HttpHandler。模块在管道中较早执行。因此,您无需与主机 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2022-01-18
    相关资源
    最近更新 更多