Page 41 - MSDN Magazine, September 2017
P. 41

Request
Response
Figure 4 ASP.NET Core Middleware Processing Pipeline
type is requested, whereas registering as transient will cause a new instance to be created for each request. Adding as scoped will cause a single instance of a service to be used throughout the processing of a single HTTP request. More details on dependency injection in ASP.NET Core are available at bit.ly/2uq0hDc.
HTTP Request-Processing Pipeline and Middleware The other important method in the Startup type is the Configure method. This is where the heart of the ASP.NET Core application— its HTTP request-processing pipeline—is set up. In this method, different pieces of middleware are registered that will act on incoming HTTP requests to generate responses.
In Startup.Configure, middleware components are added to an IApplicationBuilder to form the processing pipeline. When a request comes in, the first piece of middleware registered will be invoked. That middleware will perform whatever logic it needs to and then either call the next piece of middleware in the pipeline or, if it has completely handled the response, return to the previous piece of mid- dleware (if there was a previous one) so that it can execute any logic necessary after a response has been prepared. This pattern of calling middlewarecomponentsinorderwhenarequestarrivesandthen in reverse order after it has been handled is illustrated in Figure 4.
To take a concrete example, Figure 5 shows the Configure method from the template project. When a new request comes in, it will go first to either the DeveloperExceptionPage middleware
Figure 5 ASP.NET Core Startup.Configure Method Sets Up the Middleware Pipeline
or the ExceptionHandler middleware, depending on your envi- ronment (as before, this is configured with the ASPNETCORE_ ENVIRONMENT environment variable). These middleware components won’t take much action initially, but after subsequent middleware has run and the request is on its way back out of the middleware pipeline, they will watch for and handle exceptions.
Next, StaticFiles middleware will be called, which may serve the request by providing a static file (an image or style sheet, for example). If it does, it will halt the pipeline and return control to the previous middleware (the exception handlers). If the StaticFiles middleware can’t provide a response, it will call the next piece of middleware—the MVC middleware. This middleware will attempt to route the request to an MVC controller (or Razor Page) for fulfillment, according to the routing options specified.
The order the middleware components are registered is very important. If UseStaticFiles came after UseMvc, the applica- tion would try to route all requests to MVC controllers before checking for static files. That could result in a noticeable perf degrade! If the exception-handling middleware came later in the pipeline, it wouldn’t be able to handle exceptions occurring in prior middleware components.
Razor Pages
Besidesthe.csproj,program.cs,andstartup.csfiles,yourASP.NET Core project also contains a Pages folder containing the applica- tion’s Razor Pages. Pages are similar to MVC views, but requests can be routed directly to a Razor Page without the need for a separate controller. This makes it possible to simplify page-based applications and keep views and view models together. The model supporting the page can be included in the cshtml page directly (in a @functions directive), or in a separate code file, which is ref- erenced with the @model directive.
To learn more about Razor Pages, check out Steve Smith’s article, “SimplerASP.NETMVCAppswithRazorPages,”alsointhisissue.
Wrapping Up
Hopefully this walk-through helps explain how to create a new ASP.NET Core 2.0 Web application and demystifies the contents of the new proj- ect templates. I’ve reviewed the project contents from the streamlined .csproj file, to the application entry point and Web host configuration in Program.cs, to service and middleware registration in Startup.cs.
To keep digging deeper into what’s possible with ASP.NET Core, it might be useful to create some new projects using some of the other templates, like the Web API template or perhaps some of the new SPA templates. You may also want to try out deploying your ASP.NET Core app to Azure as an App Service Web App or packaging the application as a Linux or Windows Docker image. And, of course, please check out the full documentation at docs.microsoft.com/aspnet/core for more information on the topics covered in this article and more. n
Mike Rousos is a principal software engineer on the .NET Customer Success Team. Rousos has been a member of the .NET team since 2004, working on technologies including tracing, managed security, hosting and, most recently, .NET Core.
Thanks to the following Microsoft technical experts who reviewed this article: Glenn Condron and Ryan Nowak
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage(); }
else {
app.UseExceptionHandler("/Error"); }
app.UseStaticFiles();
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
}); }
msdnmagazine.com
September 2017 33
Middleware 1
// logic next();
// more logic
Middleware 2
// logic next();
// more logic
Middleware 3
// logic
// more logic


























































   39   40   41   42   43