通过OWIN中间件路由所有请求 [英] Route ALL requests through OWIN Middleware

查看:287
本文介绍了通过OWIN中间件路由所有请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having some troubles getting some very basic OWIN middleware to handle all requests to the IIS application. I am able to get the OWIN middleware to load per page request but I need it to handle requests for images, 404s, PDFs, and everything that could possible be typed into an address bar under a certain host name.

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

Essentially, whether I request http://localhost/some_bogus_path_and_query.jpg or http://localhost/some_valid_request, all requests would get routed through the Invoke subroutine.

使用OWIN是否可以实现?

Is this possible to achieve with OWIN?

我已经阅读了诸如(如何使用Owin中间件拦截404的线程),但我没有运气.当我真的需要OWIN在所有情况下都只写Hello World时,无论资产是否在磁盘上,IIS Express都会保留404错误.

I have read threads like (How to intercept 404 using Owin middleware) but I am not having any luck. IIS Express keeps serving up 404 errors when I really need OWIN to just write Hello World in all cases, regardless of whether an asset is on disk or not.

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

Also, I have added runAllManagedModulesForAllRequests="true" to the web.config file and I still cannot get OWIN to fire when I am requesting images via a URL.

推荐答案

您在问题中一共要求做两件事.我将尝试一个接一个地回答他们.首先,您要针对每个请求执行中间件.这可以通过在IIS集成管道中使用StageMarkers .所有中间件都在StageMarker的最后阶段即PreHandlerExecute之后执行.但是您可以指定何时执行中间件.例如.要在Middleware中获取所有传入请求,请尝试在MapHandlerPostResolveCache之前将其映射.

You have asked for couple of things altogether in your question. I'll try to answer them one by one. First you want to execute your Middleware for each request. This can be achieved by using StageMarkers within IIS integrated pipeline. All middleware executes after the last stage of StageMarker i.e. PreHandlerExecute. But you can specify when you want to execute your middleware. E.g. To get all incoming request in Middleware try mapping it before either MapHandler or PostResolveCache.

第二,您想拦截404错误重定向.在您提到的同一线程中 Javier Figueroa 在他提供的示例代码中对此进行了回答.

Second, You want to intercept the 404 error redirection. In the same thread that you mentioned; Javier Figueroa answered it in the sample code he provided.

下面是从您提到的线程中提取的同一示例:

Below is same sample taken from the thread you mentioned:

 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错误,然后执行自定义操作.

In Invoke method as you can see the response has been captured in pipeline which is already generated from IIS integrated pipeline. So the first option that you were thinking to capture is all the requests and then taking the next decision if it's 404 or not which might not work. So it's better if you capture 404 error like in the above sample and then perform your custom actions.

这篇关于通过OWIN中间件路由所有请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆