【问题标题】:Route ALL requests through OWIN Middleware通过 OWIN 中间件路由所有请求
【发布时间】:2016-03-04 11:49:22
【问题描述】:

我在获取一些非常基本的 OWIN 中间件来处理对 IIS 应用程序的所有请求时遇到了一些麻烦。我能够让 OWIN 中间件加载每个页面请求,但我需要它来处理对图像、404、PDF 以及可能在某个主机名下的地址栏中键入的所有内容的请求。

namespace HelloWorld
{
    // Note: By default all requests go through this OWIN pipeline. Alternatively you can turn this off by adding an appSetting owin:AutomaticAppStartup with value “false”. 
    // With this turned off you can still have OWIN apps listening on specific routes by adding routes in global.asax file using MapOwinPath or MapOwinRoute extensions on RouteTable.Routes
    public class Startup
    {
        // Invoked once at startup to configure your application.
        public void Configuration(IAppBuilder app)
        {
            app.Map(new PathString("/*"),
                (application) =>
                {
                    app.Run(Invoke);
                });

            //app.Run(Invoke);
        }

        // Invoked once per request.
        public Task Invoke(IOwinContext context)
        {
            context.Response.ContentType = "text/plain";
            return context.Response.WriteAsync("Hello World");
        }
    }
}

基本上,无论我请求http://localhost/some_bogus_path_and_query.jpg 还是http://localhost/some_valid_request,所有请求都将通过Invoke 子例程进行路由。

这可以用 OWIN 实现吗?

我已经阅读过 (How to intercept 404 using Owin middleware) 之类的主题,但我没有任何运气。当我真的需要 OWIN 在所有情况下编写 Hello World 时,IIS Express 会不断提供 404 错误,无论资产是否在磁盘上。

此外,我已将 runAllManagedModulesForAllRequests="true" 添加到 web.config 文件中,当我通过 URL 请求图像时,仍然无法触发 OWIN。

【问题讨论】:

  • 地图不支持通配符。返回 app.Run 并运行AllManagedModulesForAllRequests。

标签: asp.net asp.net-mvc-4 owin


【解决方案1】:

请注意,您可能还需要在 web.config 中将“runAllManagedModulesForAllRequests”设置为 true

<configuration>
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
   </system.webServer>
</configuration>

【讨论】:

    【解决方案2】:

    您在问题中总共提出了几件事。我将尝试一一回答。首先,您要为每个请求执行中间件。这可以通过using StageMarkers within IIS integrated pipeline 来实现。所有中间件在StageMarker 的最后阶段之后执行,即PreHandlerExecute。但是您可以指定何时执行中间件。例如。要在中间件中获取所有传入请求,请尝试在 MapHandlerPostResolveCache 之前对其进行映射。

    其次,你要拦截404错误重定向。在同一个thread that you mentionedJavier Figueroa 在他提供的示例代码中回答了这个问题。

    以下是从您提到的线程中提取的相同示例:

     public async Task Invoke(IDictionary<string, object> arg)
        {
            await _innerMiddleware.Invoke(arg);
            // route to root path if the status code is 404
            // and need support angular html5mode
            if ((int)arg["owin.ResponseStatusCode"] == 404 && _options.Html5Mode)
            {
                arg["owin.RequestPath"] = _options.EntryPath.Value;
                await _innerMiddleware.Invoke(arg);
            }
        }
    

    Invoke 方法中,您可以看到响应已在管道中捕获,该管道已从 IIS 集成管道生成。因此,您考虑捕获的第一个选项是所有请求,然后做出下一个决定,如果它是 404 或不是,这可能不起作用。因此,最好像上面示例中那样捕获 404 错误,然后执行自定义操作。

    【讨论】:

    • 非常感谢。我最终添加了 app.UseStageMarker(PipelineStage.PostAuthorize);到 Startup.cs 类,它就像使用 OWIN 的魅力一样。
    猜你喜欢
    • 2016-03-27
    • 2012-03-30
    • 2019-02-08
    • 1970-01-01
    • 2013-01-15
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多