ASP .NET Core Startup

Any significant application is requiring a mechanism to manage different types of requests and map them to specific services and configure services and middleware. It is accomplished by the Startup class, which is also the starting point for any incoming HTTP request.

The Design of the Startup class

The Startup class requires to have two methods:

  • Configure - this procedure will respond to each incoming HTTP request.

 

But, most real-world applications require more functionality than this. More complicated sets of pipeline configuration can be encapsulated in middleware and combined using the extension procedure of IApplicationBuilder.

Your Configure technique must accept an IApplicationBuilder parameter. Extra services, like IHostingEnvironment and ILoggerFactory, may also be specified, in which case the server will inject these services if they are available.

  • ConfigureServices- this alternative method is used for configuring services used by the application and is called before the Configure

It is the place where we will begin discussion dependency injection, which will be explaining it in a separate topic.

creating the Hello, World web application with Startup

We are going to build the same app that responds with Hello, World, this time with a Startup class and the current time.

First of all, if this is the latest application (created using .net new), you should add the dependency to the webserver Kestrel in project.json, which should look like this:

{

    "version": "1.0.0-*",

    "buildOptions": {

        "debugType": "portable",

        "emitEntryPoint": true

    },

    "dependencies": {},

    "frameworks": {

        "netcoreapp1.0": {

            "dependencies": {

                "Microsoft.NETCore.App": {

                    "type": "platform",

                    "version": "1.0.0"

                },

                "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"

            },

            "imports": "dnxcore50"

        }

    }

}

Then, add a latest file called Startup.cs and write the following code:

using System;

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Http;

 

public class Startup

{

    public void Configure(IApplicationBuilder app)

    {

        app.Run(context =>

        {

            var response = String.Format("Hello, Universe! It is {0}", DateTime.Now);

            return context.Response.WriteAsync(response);

        });

    }

}

Then, in the Main method, we indicate that we have a Startup class we want to use.

using Microsoft.AspNetCore.Hosting;

using Microsoft.AspNetCore.Builder;

 

namespace ConsoleApplication

{

    public class Program

    {

        public static void Main(string[] args)

        {

            var host = new WebHostBuilder()

                .UseKestrel()

                .UseStartup<Startup>()

                .Build();

 

                host.Run();

        }

    }

}

Now if we debug the application (from the command line or from VS Code) and goto http://localhost:5000

 on browser, we will get expected output.

Loading