Friday, February 22, 2019

Adding UseStaticFiles Middleware

The UseStaticFiles is another important middleware which is required to server static HTML, CSS or JavaScript files to the browser. In Asp.Net Core static files are placed in the wwwroot folder. In this example we shall start by adding an index.html file and serve this file to the browser using the UseStaticFiles middleware.

Let us start by adding a static HTML file to the Hello World sample, open the project and right click on the wwwroot folder Add -> New Item, in the list of file types select HTML file, give it a name index.html and create the file. Add some sample text to the file as follows.

Adding Configuration to Asp.Net Core

Configuration is another important aspect of any Application, in this post we shall see on how to add a configuration file and read the key/value pairs from the config file in the application. In traditional Asp.Net and MVC applications we had a default web.config file where we can add the config entries and use them in the application. Asp.Net core is different, here we use a JSON file for the configurations. In this post we will add a configuration file appsettings.json and read the config entries from the file at Startup.

Let us use the basic Hello World application and modify it slightly to add the configurations. First add a new JSON file to the application, name it appsettings.json. Let us add a config entry to the file as follows.

Monday, February 18, 2019

Adding UseDeveloperExceptionPage Middleware

Exception Handling is an integral part of any application, if exceptions are not handles we will see the system exception page like status code 500, this will not provide much information to the developers to investigate on the exception. In the previous .Net Framework MVC by default the stack trace and line information will be displayed in the browser, but in .Net core it shows a generic error message.

Sunday, February 17, 2019

Adding Middleware to Asp.Net Core

In the previous posts we saw a simple HelloWorld application which displays a message in the browser when the application runs. In this post we will further enhance the application by adding another Middleware to the execution sequence.