为什么这个简单的 owin 中间件会导致 403.14

Why does this simple owin middleware cause a 403.14

我正致力于为 owin 创建一些自定义中间件,但是当我 运行 它时,它在浏览器中显示 403.14。

我创建了一个空的 WebProject,添加了一个 owin 启动 class,如下所示

using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(OwinTest.Startup))]
namespace OwinTest {

    public class Startup {

        public void Configuration(IAppBuilder app) {
            app.Use<MySimpleMiddleware>();
        }

        class MySimpleMiddleware : OwinMiddleware {

            public MySimpleMiddleware(OwinMiddleware next) : base(next) { }

            public override Task Invoke(IOwinContext context) {
                context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                context.Response.ContentType = "text/plain";
                context.Response.Write("Hello, world.");

                return base.Next.Invoke(context);
            }
        }
    }
}

其中,当在 IIS express (8) 或 IIS (7.5) 中 运行 时,它向我提供 HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory. 错误。

但是,如果我把Invoke的最后一行改成return Task.FromResult(0);,那么它会按预期工作,但我不知道是否我一定要在此时终止管道(或者,如果这是 primary 中间件,我会终止吗?)

将此添加到 void Configuration(IAppBuilder app)

          app.Run(
            context =>
            {
                return context.Response.WriteAsync("More Hello, World");
            });

观察管道的简单图示here

好像不能只有中间件没有网站。 return Task.FromResult(0) 有效,因为现在您的中间件已经取代了应用程序的角色,忽略了 next 和 returning.