Getting Started With ASP.NET Core 2.1

This post is an introductory post on ASP.NET Core application development. In this post, we'll create an ASP.NET Core application and we'll examine the folders and code.

If you missed my previous post on ASP.NET Core and Benefits, take a look.

Prerequisites

To run/create applications for .NET Core, we need the following things.

  • Visual Studio 2017
  • .NET Core 2.1 SDK (for the purpose of this article only).
  • Make sure that Visual Studio 2017 has the "cross-platform development" workload installed through the Visual Studio installer.
Creating our application

For creating .NET Core applications, open Visual Studio 2017 and select File >> New >> Project. And this should launch the Templates wizard like this. 

Select as highlighted in the below screenshot.

Getting started with ASP.NET Core 2.1

The ASP.NET Core Web application template holds several other templates. Select the appropriate template and framework and hit OK.

Getting started with ASP.NET Core 2.1

Now, this should create our application. Notice that we also have templates for Angular, React, and Redux in the above screen.

Let's run the application. Press F5 or Ctrl + F5.

Getting started with ASP.NET Core 2.1

There it is. Our application is up and running. Let's examine the folder structure now. Here is what our solution looks like.

Getting started with ASP.NET Core 2.1

As you can see we also have wwwroot folder along with MVC structure. The wwwroot folder will have static files such as CSS, JS, HTML, images. This is called Web root.

To access these files, the URI format is,

http://<serveraddress>/<foldername>/<filename>

Let's try to access one image.

Getting started with ASP.NET Core 2.1

Inside of Program.cs

Here is the screenshot of the code.

Getting started with ASP.NET Core 2.1

This is just having a Main method to run the app with the default host builder. Notice we have a CreateWebHostBuilder property.

"WebHost.CreateDefaultBuilder" method sets the content root to the current directory. The Program.cs also sets the startup using .UseStartup method.

Startup.cs

Let’s see what we have in the Startup.cs class.

  1. namespace Coderethinked.Web  
  2. {  
  3.     public class Startup  
  4.     {  
  5.         public Startup(IConfiguration configuration)  
  6.         {  
  7.             Configuration = configuration;  
  8.         }  
  9.   
  10.         public IConfiguration Configuration { get; }  
  11.   
  12.         // This method gets called by the runtime. Use this method to add services to the container.  
  13.         public void ConfigureServices(IServiceCollection services)  
  14.         {  
  15.             services.Configure<CookiePolicyOptions>(options =>  
  16.             {  
  17.                 // This lambda determines whether user consent for non-essential cookies is needed for a given request.  
  18.                 options.CheckConsentNeeded = context => true;  
  19.                 options.MinimumSameSitePolicy = SameSiteMode.None;  
  20.             });  
  21.   
  22.   
  23.             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);  
  24.         }  
  25.   
  26.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  27.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
  28.         {  
  29.             if (env.IsDevelopment())  
  30.             {  
  31.                 app.UseDeveloperExceptionPage();  
  32.             }  
  33.             else  
  34.             {  
  35.                 app.UseExceptionHandler("/Home/Error");  
  36.                 app.UseHsts();  
  37.             }  
  38.   
  39.             app.UseHttpsRedirection();  
  40.             app.UseStaticFiles();  
  41.             app.UseCookiePolicy();  
  42.   
  43.             app.UseMvc(routes =>  
  44.             {  
  45.                 routes.MapRoute(  
  46.                     name: "default",  
  47.                     template: "{controller=Home}/{action=Index}/{id?}");  
  48.             });  
  49.         }  
  50.     }  
  51. }  

The ConfigureServices method is called by the runtime as indicated in the comments above the method. This method can be used to add services.

Ex,

  1. services.AddAntiforgery();  
  2. services.AddCors();  
  3. services.AddAuthorization();  
  4. services.AddAuthentication();  

There are several other options to add into this Configure services method.

Configure

This method is used to configure what the application needs.

Notice the configure method. Here, we have the following statement.

  1. app.UseStaticFiles();  

This is responsible for serving the static files in our application.

If we comment that out, we cannot access the static resources anymore. We'll see all our images, JS, and CSS will be stopped and our application looks like this.

Getting started with ASP.NET Core 2.1

Things like SignalR, Routing, and third-party authentication can be configured here. 

Launch Settings

This JSON file holds the configurations to run the application.

Getting started with ASP.NET Core 2.1

As we are running the application on IIS Express, the necessary configuration for IIS Express is utilized.

  1. "iisSettings": {  
  2.     "windowsAuthentication"false,   
  3.     "anonymousAuthentication"true,   
  4.     "iisExpress": {  
  5.       "applicationUrl""http://localhost:51681",  
  6.       "sslPort": 44307  
  7.     }  
  8.   }  

In the above JSON, we have application URL: "http://localhost:51681" and an SSL Port: 44307 allocated. So, 44307 is for SSL port meaning this is for accessing site with HTTPS://. So https://localhost:44307 is secured while http://localhost:51681 is just a plain HTTP which is not secured.

So, HTTP and HTTPS are configured by default.

The other folders like Controllers, Models, Views are with the other MVC projects we have encountered before. I have not found anything special in those files.

Thanks for reading.