Why MVC handler didn't execute after IAppBuilder.Run method invoked?

大憨熊 提交于 2019-12-13 03:36:35

问题


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?

Here are my implementations:

Case 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>");
        });

      }
   } 

Case 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>");
         });

      }
    }

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?


回答1:


You can find info about Run method here

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

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.

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   
        });

Case 1 will return only hello world

Case 2 will append hello world



来源:https://stackoverflow.com/questions/32676616/why-mvc-handler-didnt-execute-after-iappbuilder-run-method-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!