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

In MVC5, which is integrated with OWIN (Katana) via Microsoft.Owin.Host.SystemWeb.dll, why did the MVC handler fail to execute after IAppBuilder.Run , and was executed after the IAppBuilder.Use method called?

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 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, the OWIN component runs as an HttpModule (OwinHttpModule), which is registered in the Asp.NET pipeline. So, why did the IAppBuilder.Run method skip the MVC module?

0
source share
1 answer

You can find information about the Run method here.

Inserts middleware into the OWIN pipeline that does not have the following middleware link

The MVC handler executes after all owin intermediate components (OMC), so it skipped. Use method simply adds a new module. The MVC handler will start after all modules. await next.Invoke will wait for the next OMC to complete. Read these articles.

Try experimenting 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 add hello world

+2
source

Source: https://habr.com/ru/post/1232070/


All Articles