Menu Close

ASP.Net Core Hello World Program

In this article we will learn about ASP .Net Core Hello World Program. Please read my previous article Introduction to ASP.Net Core.

In this article, I will explain how write a Hello World program in Visual Studio 2017 with ASP.net core.
Step 1: Visual Studio 2017–>File->New–>Project

  •  I choose C# as code and ASP .Net Core as application. 
  • Also choose the folder path where want to save your project.
  • The project name i set as “HelloWorld”.
  • Then Click OK

Select the empty template as like below:

  • I choose ASP .Net Core 2.2
  • Project template as Empty
  • Also unchecked the Configure HTTPS button as I run the application in http only, if you want to run it on HTTPS then you can select the checkbox. At anytime we should configure HTTP and HTTPS in project configuration we should discuss it later blog.

Folder structure of ASP.Net Core

hello-world-aspnet-core
  • You can see there is showing “appsettings.json” through you can configure your project settings. 
  • When the project will run it first hit the “Programm.cs” file and it navigate it into “Startup.cs” file.
  • Please follow the link to see the Project structure of Asp.Net Core.

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace HelloWorld
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

Startup.cs

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Threading.Tasks;  
 using Microsoft.AspNetCore.Builder;  
 using Microsoft.AspNetCore.Hosting;  
 using Microsoft.AspNetCore.Http;  
 using Microsoft.Extensions.DependencyInjection;  
 namespace HelloWorld  
 {  
   public class Startup  
   {  
     // This method gets called by the runtime. Use this method to add services to the container.  
     // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940  
     public void ConfigureServices(IServiceCollection services)  
     {  
     }  
     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
     public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
     {  
       if (env.IsDevelopment())  
       {  
         app.UseDeveloperExceptionPage();  
       }  
       app.Run(async (context) =>  
       {  
         await context.Response.WriteAsync("Hello World!");  
       });  
     }  
   }  
 }  

“env.IsDevelopment” refers that the project run in development mode, you can change it into Production, staging mode accordingly.

Now run the application with VS start button or F5 and see the result.

Conclusion

In this article we learnt about ASP.Net Core Hello World Program.

Leave a Reply

Your email address will not be published. Required fields are marked *