Basics Of ASP.NET Core Using Visual Studio 2017 - Part Two

Introduction

This article explains about ASP.NET Core using Visual Studio 2017 and how to create ASP.NET Core applications in simple ways. Before reading this article, please read the previous part of this article at the below link.

Summary of the previous article

The previous part explained Kestrel, Content Root, Startup class, Application Insights, and Dependencies. This article continues that information and will explain about Configure method in a detailed way and how to develop a simple application with a simple example page.

Configure Method

This is a void method and it is called at run time. We use this method to configure the HTTP request pipeline. The configured class contains three parameters and these three parameters are an interface.

  1. public void Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerfactory)  
  2. {  
  3.     //Codings  
  4. }  

IApplicationBuilder

IApplicationBuilder is an interface. It's name space is “Microsoft.AspNetCore.Builder”. It defines a class that provides the mechanisms to configure an application's request pipeline. IApplicationBuilder contains properties and methods.

  1. namespace Microsoft.AspNetCore.Builder  
  2. {  
  3.       
  4.     public interface IApplicationBuilder  
  5.     {  
  6.         IServiceProvider ApplicationServices { get; set; }  
  7.         IFeatureCollection ServerFeatures { get; }  
  8.         IDictionary<string, object> Properties { get; }  
  9.         RequestDelegate Build();  
  10.         IApplicationBuilder New();  
  11.         IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware);  
  12.     }  
  13. }  

IHostingEnvironment

IHostingEnvironment is an interface and it's name space is Microsoft.AspNetCore.Hosting”. It provides information about the web hosting environment an application is running on. It contains many properties; we can see the properties in the below code.

  1. namespace Microsoft.AspNetCore.Hosting  
  2. {  
  3.     public interface IHostingEnvironment  
  4.     {  
  5.           
  6.         string EnvironmentName { get; set; }  
  7.         string ApplicationName { get; set; }  
  8.         string WebRootPath { get; set; }  
  9.         IFileProvider WebRootFileProvider { get; set; }  
  10.         string ContentRootPath { get; set; }  
  11.         IFileProvider ContentRootFileProvider { get; set; }  
  12.     }  
  13. }  

ILoggerFactory

ILoggerFactory is another one of the interfaces. It's name space is “Microsoft.Extensions.Logging”.  It represents a type used to configure the logging system and create instances of ILogger from the registered ILoggerProviders.

  1. namespace Microsoft.Extensions.Logging  
  2. {  
  3.     public interface ILoggerFactory : IDisposable  
  4.     {  
  5.           
  6.         void AddProvider(ILoggerProvider provider);  
  7.         ILogger CreateLogger(string categoryName);  
  8.     }  
  9. }  

Run Application

We will simply build and run our demo ASP.NET Core application. Our demo application does not have any simple code right now. We are going to add some set of coding at Configure method in the Startup class, then run our application.

  1. namespace DemoASP.NETCore  
  2. {  
  3.     public class Startup  
  4.     {  
  5.          
  6.         public void ConfigureServices(IServiceCollection services)  
  7.         {  
  8.         }  
  9.   
  10.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  11.         public void Configure(  
  12.             IApplicationBuilder app,  
  13.             IHostingEnvironment env,  
  14.             ILoggerFactory loggerFactory  
  15.             )  
  16.         {  
  17.               //Run application using interface parameter app  
  18. app.Run(async (context) =>  
  19.             {  
  20.                 await context.Response.WriteAsync("Hello World!");  
  21.             });  
  22.         }  
  23.     }  
  24. }  

Here, configure the middleware compound using the Run method in Configure static method. We add some text to identify whether the middleware component responds or not. Now, we run the application in Internet Explorer or Microsoft Edge. After running the application, we can see that it looks like the below screenshot.

ASP.NET Core
Now, use this code to configure middlewere compounded in Configure method.

  1. public void Configure(  
  2.             IApplicationBuilder app,  
  3.             IHostingEnvironment env,  
  4.             ILoggerFactory loggerFactory  
  5.             )  
  6.         {  
  7.               
  8.             //app.Run(async (context) =>  
  9.             //{  
  10.             //    await context.Response.WriteAsync("Hello World!");  
  11.             //});  
  12.         }  

Now, build and run the demo application. We are getting "HTTP 404 Not Found" error because we did not configure any middleware in the pipeline. We can see the error in the below screenshot.

ASP.NET Core

Exception

We can easily handle the exceptions in ASP.NET Core. Whenever we get an exception, we can get more details to find the exceptions. For example, we add the following code in static Configure method.

  1. public void Configure(  
  2.             IApplicationBuilder app,  
  3.             IHostingEnvironment env,  
  4.             ILoggerFactory loggerFactory  
  5.             )  
  6.         {  
  7.             
  8.             app.Run((context) =>  
  9.             {  
  10.                 //await context.Response.WriteAsync("Hello World!");  
  11.                 //below line force to throw exception  
  12.                 throw new Exception();  
  13.             });  
  14.         }  

Now, build and run the demo core applications. We are getting "HTTP 500 Internal Server Error" but here, we did not get an exact error message or do not have any way to find it.

ASP.NET Core

We are adding Use Developer Exception Page in the middleware compound in configure method. The below code is adding the developer exception page.

  1. public void Configure(  
  2.             IApplicationBuilder app,  
  3.             IHostingEnvironment env,  
  4.             ILoggerFactory loggerFactory  
  5.             )  
  6.         {  
  7.   
  8.             if (env.IsDevelopment())  
  9.             {  
  10.                 app.UseDeveloperExceptionPage();  
  11.             }  
  12.   
  13.             app.Run((context) =>  
  14.             {  
  15.                 //await context.Response.WriteAsync("Hello World!");  
  16.                 throw new Exception();  
  17.             });  
  18.         }  

Using interface parameter env, it checks that  current application is developed and it makes sure it is not a production. Now, build and run the application. We can see the detailed message of exception; it helps to find errors. The below error page contains Stack, Query, Cookies, and headers.

ASP.NET Core

We can see the Stacks, Query, Cookies and Header tabs in the below screenshots.

Stacks

ASP.NET Core

Query
ASP.NET Core
Cookies

ASP.NET Core
Headers

ASP.NET Core
Conclusion

This article explained about the basic of the ASP.NET Core. The above-mentioned basics can really help the developers at all levels.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now