ASP.Net Core MVC Project Part 1

In this article we will see steps for creating ASP.Net Core 3.1 MVC Project. There will be multiple articles for this project and will cover step by step demonstration of the project. This is the first article in series.

We are using Visual Studio 2019 for this project. You need to install community edition of Visual Studio 2019, please see below URL for Visual Studio installation:
https://visualstudio.microsoft.com/vs/

You also need to install SQL Server 2014 or higer versions of SQL Server for this project. Below is the URL for SQL Server 2014 express edition:
https://www.microsoft.com/en-in/download/details.aspx?id=42299

Open Visual Studio, and click on “Create a new project” option :

Select “ASP.Net Core Web Application” project template and click on Next button. You can also search if template is not visible.

Give a name to the project and click on Create button :

First drop down is displaying .Net Core. Now select ASP.Net Core 3.1 from the second drop down and select empty project template. We will create everything from scratch.
Now leave all other settings as it is and click on Create button:

Now project is created :

Now we will have a quick look of the project. First we will explore Program.cs

Program.cs

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

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

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

You can see, we have a static void Main() method, this is the method which runs first when application starts similar to other C# programs.
Main() method calls another static method CreateHostBuilder() and this method returns object of type IHostBuilder. This method creates a host which will run our application. First we call CreateDefaultBuilder() method which comes with some defaults and one of those defaults is appsettings.json file.

Below is the content of appsettings.json file.

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

This file is read by application when it starts so application expects this file and it reads automatically.We will use appsettings.json to store connection string for database connectivity. After the web host is created , it will be configured by method ConfigureWebHostDefaults(). Please see method CreateHostBuilder() in Program.cs file.
ConfigureWebHostDefaults() method contains several defaults that are automatically implemented.It will create a web server called Kestrel.Server used for deployment is IIS and it is also configured in this method. The last method which is called in this configuration is UseStartup(), it is taking Startup as a type of parameter.Startup class is defined in Startup.cs class. Below is the code of Startup class.

Startup.cs

public class Startup
{
 // This method gets called by the runtime. 
  public void ConfigureServices(IServiceCollection services)
  {
  }

 // This method gets called by the runtime. 
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }

Startup class has 2 important methods ConfigureServices() and Configure(). These methods are called by .Net Core automatically. ConfigureServices() method uses collection of type IServiceCollection. In this method all services are added including built-in services as well as our own services that we will register here.

Download code from below URL:
https://github.com/vijendra-ksh/ASP.NetCoreMVC_Project_Part1

Other articles in this series:

ASP.Net Core MVC Project Part 2
ASP.Net Core MVC Project Part 3
ASP.Net Core MVC Project Part 4
ASP.Net Core MVC Project Part 5
ASP.Net Core MVC Project Part 6

Please like our Facebook page for getting new posts update.
https://www.facebook.com/logixn

Leave a Comment

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