Notes from building a first ASP.Net Core App (part 4)

  1. ASP.Net Core applications are layered. There is an application pipeline that processes your http requests and responses. This is known as middleware.
  2. The logger factory is a piece of middleware, as is the Developer Exception page.You can find them in the Configure method of the Startup class in Startup.cs.
    if (env.IsDevelopment())
  3. app.Run is a piece of middleware as well. The context object is similar to the HttpContext object you would be familiar with. It has references to Request and Response objects. app.Run occurs at the end of the pipeline. Nothing after app.Run will be executed, as it does not allow piping into another piece of middleware.
  4. To register your own middleware, you’d add it via the app.Use method. There are some provided by default. There are others you can load from nuget packages.
  5. For example, if you want to add a Welcome Page, you can add the following line just before calling app.Run:

    This provides a default welcome page. If you run the site, there will now be a Welcome Page. There are overrides for this method that will allow you to supply alternative welcome pages. Most pipelines do have overrides to supply configuration.

  6. When you’ve finished with the Welcome Page, comment that line out.
  7. One of the default pipelines available is the Hosting Environment pipeline. It is here that you can query to find out whether you are in development, staging or production, what the Application Name is, what the Content Root Path is, what the absolute Web Root Path is.This is injected via the IHostingEnvironment interface in the Configure method. It knows which environment you are in by looking at the Project’s Environment Variables, which you set from the project’s debug configuration page.
  8. Another one of the default pipelines supplied is the Exception Handling page for developers. Without it, your pages would get an Internal Server Error (status code of 500.)
  9. Go into the app.Run call and add an Exception, as follows:
    app.Run(async (context) =>
        throw new Exception("What!!!");
        var newHelloWorldMessage = string.Format("Hello World! Your text was >{0}<", myCustomConfiguration.GetMyCustomMessage());
        await context.Response.WriteAsync(newHelloWorldMessage);

    Then run your application. Because of the exception handler pipeline, it will display an Exception page, as follows: asp-net-core-exception
    If you choose to, you can click the “Show raw exception details link” and it will expand and provide a stack trace.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: