Javascript required
Skip to content Skip to sidebar Skip to footer

How to Open Asp Net Project in Visual Studio 2017

Introduction

This article explains the basics of ASP.NET Core using Visual Studio 2017 and easy ways to create ASP.NET Core applications.

.NET Core

.NET Core is an open source and cross-platform subset of the full .NET framework and it supports Windows, Linux, and macOS. It is used to develop Windows applications as well as Console applications, and it only supports single app model.

ASP.NET Core

ASP.NET Core is a new generation of ASP.NET. It is used to develop web applications with better and improved features. ASP.NET Core is free, open source, and cross-platform development framework. We can develop and run ASP.NET Core apps on Windows, Mac, and Linux. ASP.NET Core is not based on System.Web.dll, it is fully different. There are three important .NET Core tools as follows.

  • Visual Studio 2017
  • Visual Studio Code
  • Command Line Interface.

We can install Visual Studio 2017 in very easy way following the steps mentioned in the article Install Visual Studio 2017.

Simple example and overview of ASP.NET Core Application

We are using Visual Studio 2017 for developing ASP.NET Core application. Open Visual Studio, open New Project >> "ASP.NET Core Web Application (.NET Core)".

ASP.NET Core

We selected "ASP.NET Core 1.1" and then Empty template. An empty project template is selected for creating an ASP.NET Core application. This template does not have any content in it.

ASP.NET Core

Solution Explorer

We can see all the folders and files in Solution Explorer. These files are different as compared to the normal ASP.NET files.

ASP.NET Core

Dependencies are like references in ASP.NET Core. If we expand the dependencies, we can see the NuGet and SDK.  ASP.NET Core is a full NuGet package and it is not based on System.Web.dll.

We can see the SDK and inside SDK, we can see the "Microsoft.NETCore.App". It is a target to .NET Core framework.

ASP.NET Core

All the NuGet packages are added as references. Right click your project in Solution Explorer and go to Edit DemoASP.NET Core.csproj.

ASP.NET Core

Now, DemoASP.NET.Core.csproj file will open. We can see the package reference in that file which looks like the below screenshot.

ASP.NET Core

If we remove any one of the package references from DemoASP.NET.Core.csproj, it will be removed from NuGet in Solution Explorer too.

ASP.NET Core
Program.cs

Program class is the main part of the ASP.NET Core. It contains static method and also beginning pointing of the application. This main method creates a host, builds a host, and runs the host.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Hosting;
  7. namespace DemoASP.NETCore
  8. {
  9. public class  Program
  10.     {
  11. public static void  Main(string[] args)
  12.         {
  13. var  host = new  WebHostBuilder()
  14.                 .UseKestrel()
  15.                 .UseContentRoot(Directory.GetCurrentDirectory())
  16.                 .UseIISIntegration()
  17.                 .UseStartup<Startup>()
  18.                 .UseApplicationInsights()
  19.                 .Build();
  20.             host.Run();
  21.         }
  22.     }
  23. }

Kestrel

Kestrel is a cross-platform web server for ASP.NET Core.  Kestrel is the web server that is included by default in ASP.NET Core project templates. It supports HTTPS.

  1. public static void  Main(string[] args)
  2.         {
  3. var  host = new  WebHostBuilder()
  4.                 .UseKestrel()
  5.                 .UseContentRoot(Directory.GetCurrentDirectory())
  6.                 .UseIISIntegration()
  7.                 .UseStartup<Startup>()
  8.                 .UseApplicationInsights()
  9.                 .Build();
  10.             host.Run();
  11.         }

Content Root

Content Root specifies the content root directory to be used by the web host. It gets the current root path of our project. For example, our project root path is http://localhost:53498/.

IIS Integration

IIS configures the port and base path the server should listen to when running behind ASP.NET Core Module. The app will also be configured to capture startup errors.

  1. var  host = new  WebHostBuilder()
  2.                 .UseKestrel()
  3.                 .UseContentRoot(Directory.GetCurrentDirectory())
  4.                 .UseIISIntegration()
  5.                 .UseStartup<Startup>()
  6.                 .UseApplicationInsights()
  7.                 .Build();
  8.             host.Run();

Build and Run

Build is a Microsoft.AspNetCore.Hosting.IWebHost which hosts a web application. Run is used to run our application. Our application is hosted in the host.

  1. var  host = new  WebHostBuilder()
  2.                 .UseKestrel()
  3.                 .UseContentRoot(Directory.GetCurrentDirectory())
  4.                 .UseIISIntegration()
  5.                 .UseStartup<Startup>()
  6.                 .UseApplicationInsights()
  7.                 .Build();
  8.             host.Run();

Startup Class

Startup class is a special class and it does not have any inheritance, interface, and over loading. It is mainly used to start the application.

  1. namespace DemoASP.NETCore
  2. {
  3. public class  Startup
  4.     {
  5. public void  ConfigureServices(IServiceCollection services)
  6.         {
  7.         }
  8. public void  Configure(
  9.             IApplicationBuilder app,
  10.             IHostingEnvironment env,
  11.             ILoggerFactory loggerFactory
  12.             )
  13.         {
  14.             app.Run(async (context) =>
  15.             {
  16.                 await context.Response.WriteAsync("Hello World!" );
  17.             });
  18.         }
  19.     }
  20. }

The startup class has two methods - ConfigureServices and Configure. ConfigureServices method is used to configure the services and is called at runtime. We can configure normal services, third party services, third party compounds, tools and filters for MVC. We will see this later.

Configure method is used to configure the HTTP request pipeline.

There are three main arguments as interfaces -  IApplicationBuilder, IHostingEnvironment, ILoggerFactory.

IApplicationBuilder defines a class that provides the mechanisms to configure an application's request pipeline. IHostingEnvironment provides information about the web hosting environment an application is running in. ILoggerFactory represents a type used to configure the logging system and create instances of ILogger from the registered ILoggerProviders.

We are configuring single call using App.Run. App.Run is configuring terminal middleware compound. Middleware compound handles HTTP request.  We are going to do a simple thing here: handle HTTP request and wait for a response to async call so as to write a simple text "Hello World".

ASP.NET Core
Conclusion

This article explained the basics of the ASP.NET Core.

How to Open Asp Net Project in Visual Studio 2017

Source: https://www.c-sharpcorner.com/article/basics-in-asp-net-core-using-visual-studio-2017-part-one/