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.
- public void Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerfactory)
- {
-
- }
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.
- namespace Microsoft.AspNetCore.Builder
- {
-
- public interface IApplicationBuilder
- {
- IServiceProvider ApplicationServices { get; set; }
- IFeatureCollection ServerFeatures { get; }
- IDictionary<string, object> Properties { get; }
- RequestDelegate Build();
- IApplicationBuilder New();
- IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware);
- }
- }
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.
- namespace Microsoft.AspNetCore.Hosting
- {
- public interface IHostingEnvironment
- {
-
- string EnvironmentName { get; set; }
- string ApplicationName { get; set; }
- string WebRootPath { get; set; }
- IFileProvider WebRootFileProvider { get; set; }
- string ContentRootPath { get; set; }
- IFileProvider ContentRootFileProvider { get; set; }
- }
- }
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.
- namespace Microsoft.Extensions.Logging
- {
- public interface ILoggerFactory : IDisposable
- {
-
- void AddProvider(ILoggerProvider provider);
- ILogger CreateLogger(string categoryName);
- }
- }
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.
- namespace DemoASP.NETCore
- {
- public class Startup
- {
-
- public void ConfigureServices(IServiceCollection services)
- {
- }
-
-
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
-
- app.Run(async (context) =>
- {
- await context.Response.WriteAsync("Hello World!");
- });
- }
- }
- }
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.
Now, use this code to configure middlewere compounded in Configure method.
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
-
-
-
-
-
- }
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.
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.
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
-
- app.Run((context) =>
- {
-
-
- throw new Exception();
- });
- }
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.
We are adding Use Developer Exception Page in the middleware compound in configure method. The below code is adding the developer exception page.
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
-
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.Run((context) =>
- {
-
- throw new Exception();
- });
- }
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.
We can see the Stacks, Query, Cookies and Header tabs in the below screenshots.
Stacks
Query
Cookies
Headers
Conclusion
This article explained about the basic of the ASP.NET Core. The above-mentioned basics can really help the developers at all levels.