为什么调用IAppBuilder.Run方法后MVC处理程序没有执行? [英] Why MVC handler didn't execute after IAppBuilder.Run method invoked?

查看:217
本文介绍了为什么调用IAppBuilder.Run方法后MVC处理程序没有执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC5,它通过Microsoft.Owin.Host.SystemWeb.dll,为什么MVC处理程序后,<一个没有执行与OWIN(武士刀)综合href=\"https://msdn.microsoft.com/en-us/library/owin.appbuilderuseextensions.run(v=vs.113).aspx#M:Owin.AppBuilderUseExtensions.Run(Owin.IAppBuilder,System.Func%7BMicrosoft.Owin.IOwinContext,System.Threading.Tasks.Task%7D)\"相对=nofollow> IAppBuilder.Run 方法调用,在<一个没有执行href=\"https://msdn.microsoft.com/en-us/library/owin.appbuilderuseextensions.use(v=vs.113).aspx#M:Owin.AppBuilderUseExtensions.Use(Owin.IAppBuilder,System.Func%7BMicrosoft.Owin.IOwinContext,System.Func%7BSystem.Threading.Tasks.Task%7D,System.Threading.Tasks.Task%7D)\"相对=nofollow> IAppBuilder.Use 方法调用?

In MVC5, which integrated with OWIN(Katana) via Microsoft.Owin.Host.SystemWeb.dll, why MVC handler didn't execute after IAppBuilder.Run method invoked, and did execute after IAppBuilder.Use method invoked?

下面是我实现的:

案例1:

   public partial class Startup
   {
      public void Configuration(IAppBuilder app)
      {
        app.Use( async (context, next) =>
        {
            await context.Response.WriteAsync(@"<h1>Before MVC</h1>");

            await next.Invoke();  //What 's the "next" object now?

            await context.Response.WriteAsync(@"<h1>After MVC</h1>");
        });

      }
   } 

案例2:

    public partial class Startup
    {
      public void Configuration(IAppBuilder app)
      {
         app.Run( async context => 
         {
             await context.Response.WriteAsync(@"<h1>MVC has not been invoked.</h1>");
         });

      }
    }

在这种情况下,我知道,OWIN组件运行的HTTP模块(OwinHttpModule),其中登记在Asp.NET管道。
那么,为什么IAppBuilder.Run方法的执行导致MVC模块的跳跃?

In this case, as i know, OWIN component run as HttpModule(OwinHttpModule), which registered in Asp.NET pipeline. So why Execution of IAppBuilder.Run method led to skipping of MVC module?

推荐答案

您可以找到有关运行方法的这里

You can find info about Run method here

插入到OWIN管道不具有下一个中间件
  中间件参考

Inserts into the OWIN pipeline a middleware which does not have a next middleware reference

MVC处理程序执行毕竟owin中间件组件(的OMC),这就是为什么它跳过。使用方法,只需添加新的模块。 MVC处理程序将所有模块之后运行。 等待next.Invoke 将等待下一个OMC的执行。读<一个href=\"http://www.asp.net/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline\"相对=nofollow>这个的文章。

MVC handler executed after all owin middleware components (OMCs), that's why it skipped.Use method just add new module. MVC handler will run after all modules. await next.Invoke will wait for execution of the next OMC. Read this articles.

尝试实验与此code

Try experiment with this code

        app.Use((context, next) =>
        {              
             context.Response.WriteAsync("Hello, world.");
             return Task.FromResult(0);                         //case 1
            //return next.Invoke();                             //case 2   
        });

案例1只返回的Hello World

第2种情况将追加的Hello World

这篇关于为什么调用IAppBuilder.Run方法后MVC处理程序没有执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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