Asp.Net Core2.0 + Vs2017 + MVC learning two middleware

Continue with Configure method, for example after the last modification

public  void the Configure (IApplicationBuilder App, IHostingEnvironment env, 
            IWelcomeSeriver welcomeSeriver) // You can also customize the interface ,, dependency injection, attention must be registered with the custom interface, or can not find 
        {
             IF (env.IsDevelopment ()) 
            { 
                app.UseDeveloperExceptionPage (); 
            } 

            app.run ( the async (context) => 
            { 
                String available for purchase welcomeSeriver.GetWelcomeMessage = (); // here for a profile should be a priority command line> system variables> development environment profile> profile 
                await context.Response.WriteAsync (available for purchase); 
            }); 
        }

In a real project, app.Run rarely used method, this method is in general only a simple configuration of middleware.

----------------------------------------------------------------------------------------------------------------------------

Usually, the app.UseXXXX method, and the corresponding method is also generally regarded encapsulated achieved, and it is also very convenient to use.

E.g. app.UseWelcomePage (); code execution to it, it returns a welcome page truncated, the route may be provided welcome page.

app.UseWelcomePage(new WelcomePageOptions {
                Path = "/MyWelcome"
            });

Access localhost: When xxxx / MyWelcome, return to a welcome page.

--------------------------------------------------------------------------------------------------------------------------------

Also can be used directly app.Use (), write an example here

public  void the Configure (App IApplicationBuilder, IHostingEnvironment the env, IConfiguration Configuration) 
        { 
            // IF (env.IsDevelopment ())
             // {
             //     app.UseDeveloperExceptionPage ();
             // }
             // Next RequestDelegate an object that represents the pipe where a next intermediate 
            app.Use (next => 
            { 
                return  the async httpContext => 
                { 
                    String STR = httpContext.Request.Path;
                     // if login path, PLS returns login! 
                    IF (str.ToLower (). the Contains ( " the Login" )) 
                    { 
                        The await httpContext.Response.WriteAsync ( " Login PLS! " ); 
                    } 
                    // If not, the next one intermediate 
                    the else 
                    { 
                        the await Next (httpContext); 
                    } 
                }; 
            }); 

            // If the path is / MyWelcome, return to welcome page, not the next 
            app.UseWelcomePage ( new new WelcomePageOptions { 
                the Path = " / MyWelcome " 
            }); 

            // if this execution to, reads the configuration file from DefaultWords output node out 
            app.run (async (context) =>
            {
                string str = configuration["DefaultWords"];
                await context.Response.WriteAsync(str);
            });
        }

Here can also be seen, a plurality of intermediate is performed in the order (if it is not interrupted)

 

Guess you like

Origin www.cnblogs.com/luyShare/p/11599384.html