Sunday, February 17, 2019

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.


Ø  Open Visual Studio
Ø  Select File -> New -> Projects
Ø  Under Visual C# -> Web select Asp.Net Core Web Application



Ø  In the list of templates, select Empty and click OK


Ø  This will create a simple project with 3 files, and other supporting folders (/bin, /obj, /wwwroot)
o   Startup.cs
o   Program.cs
o   HelloAspNet.csproj

Ø  The Startup.cs is the main file which we are interested in, it contains the Configure() method which defines the execution pipeline for the application. The empty template which we selected by default displays Hello World in the browser when the application executes. The code for Startup.cs looks as follows.


namespace HelloAspNet
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

Build and run the application, a browser opens up and prints Hello World!
The context.Response.WriteAsync line prints Hello World! in the browser.



Search Flipkart Products:
Flipkart.com

No comments: