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.

Hello World example in Asp.Net Core

In the previous posts we saw about what Asp.Net Core is, its features and its advantages. Let us get started with a simple Hello World example in Asp.Net Core. We will use Visual Studio for this example. We can use Visual Studio Code also for a simple example like this, but for complex example Visual Studio is a better tool, hence we will use Visual Studio for this example.

Before we begin the example make sure that .Net Core SDK is installed in the machine, also install Visual Studio. In this example we will use Visual Studio 2017. Let’s get started.

Sunday, February 10, 2019

Asp.Net Core Host and Logging Configuration

A simple Asp.Net core application contains a Startup.cs file and a Program.cs file. We saw that the Startup file is used to define the execution pipeline to process Http requests and to inject services. In this post we shall see about the role of Program.cs file.

The Program.cs file in an Asp.Net core application contains logic to define the host configuration and logging configuration. It has the public static void Main() method which is the entry point of the application.

The Main() method calls CreateWebHostBuilder, which in-turn calls WebHost.CreateDefaultBuilder, this method performs the following tasks under the hood.

Asp.Net Core Startup Configuration

Startup configuration for Asp.Net Core applications are defined in a startup file Startup.cs. This is similar to the global.aspx.cs file in the previous version of Asp.Net. Startup settings are important in Asp.Net Core since we need to define the execution pipeline during startup to handle all incoming requests.

There are 2 important methods in the Startup which define the execution sequence of Asp.Net core request execution.

Configure
ConfigureServices

Monday, February 4, 2019

Advantages of Asp.Net Core

Asp.Net core is a light weight, cross-platform compatible web development framework from Microsoft. Asp.Net core can be considered as the next generation web development framework from Microsoft, Asp.Net for long has been windows centric, for the first time Asp.Net core has made web application development possible in multiple platforms. The other key improvement in Asp.Net core is that it can produce light weight applications with higher performance compared to the previous version of Asp.Net. Let us look into some of the advantages of Asp.Net core in detail.

What is Asp.Net Core

Asp.Net core is a new web development framework from Microsoft providing cross platform compatibility. The previous version of Asp.Net could be developed in Windows and deployed in IIS. Asp.Net core changes this, Asp.Net core applications can be developed in Windows, Linux or Mac OS.

Asp.Net core also aims in producing leaner light weight applications compared to traditional Asp.Net applications. Asp.Net core provides more flexibility in the way we design our applications, it allows us to chain a set of Middle-ware's to process the Http request.

Each middle-ware performs a specific task and passes on the result to the next Middle-ware in the chain. Each piece of middle-ware in ASP.NET Core is an object, and each piece has a very specific, focused, and limited role.