Files And Folders - ASP.NET MVC Core 3.0

Introduction 
 
Today, we will learn the files and folders structure of an ASP.NET MVC Core 3.0 application. When we create an MVC core application, Visual Studio 2019 will respective files and folders which will discuss in this article. In my last article, I discussed ASP.NET core, what it is, and what is the benefit of using it. I recommend that you check it out.
Step 1
 
Start your Visual Studio 2019. Choose ASP.NET Core Web Application and click on “Next”
 
 
After clicking next, another wizard will open. Under the project name give a meaningful name to your project and click on create.
 
 
That will open up another wizard. Select ASP.Net Core 3.0 from dropdown if not select default. Choose Web Application (Model-View-Controller) template and click on create which will create your ASP.Net Core Application.
 

Files and Folders

  • wwwroot
  • Controllers
  • Models
  • Views
  • appsettings.json
  • Program.cs
  • Startup.cs
wwwrotFolder
 
This folder contains CSS folder, JS folder lib folder which contains all JavaScript and styles file in wwwroot folder.
 
Controllers Folder
 
This folder contains the default Home controller which has an index and privacy public method.
 
Models Folder
 
This folder contains ErrorViewModel which configures for error handling.
 
View Folder 
 
This folder contains a Home folder. Under the home folder there are 2 view files: Index and privacy. It also contains a share folder. Under this share folder there are _Layout.cshtml, _ValidationScriptsPartial.cshtml and Error.cshtml view files. There are two more view files, _ViewImports.cshtml and _ViewStart.cshtml under the view folder. I will discuss this file in upcoming articles when I talk about _Layout.cshtml etc.
 
appsettings.json
 
appsettings.json file in ASP.NET Core project. In previous versions of ASP.NET, we store application configuration settings, like database connection strings for example, in web.config file. In ASP.NET Core application configuration settings can come from the following different configurations sources. 
  1. {  
  2.   "Logging": {  
  3.     "LogLevel": {  
  4.       "Default""Information",  
  5.       "Microsoft""Warning",  
  6.       "Microsoft.Hosting.Lifetime""Information"  
  7.     }  
  8.   },  
  9.   "AllowedHosts""*"  
  10. }  
Program.cs
 
In an ASP.NET Core project we have a file with name Program.cs. In this file we have a public static void Main() method.
 

Why do we have a Main() method?

 
The important point to keep in mind is that an ASP.NET core application initially starts as a console application and the Main() method in Program.cs file is the entry point. So, when the runtime executes our application it looks for this Main() method where the execution starts. This Main() method configures asp.net core and starts it. At that point, it becomes an ASP.NET Core web application.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Hosting;  
  6. using Microsoft.Extensions.Configuration;  
  7. using Microsoft.Extensions.Hosting;  
  8. using Microsoft.Extensions.Logging;  
  9.   
  10. namespace MvcCoreFirstApplication  
  11. {  
  12.     public class Program  
  13.     {  
  14.         public static void Main(string[] args)  
  15.         {  
  16.             CreateHostBuilder(args).Build().Run();  
  17.         }  
  18.   
  19.         public static IHostBuilder CreateHostBuilder(string[] args) =>  
  20.             Host.CreateDefaultBuilder(args)  
  21.                 .ConfigureWebHostDefaults(webBuilder =>  
  22.                 {  
  23.                     webBuilder.UseStartup<Startup>();  
  24.                 });  
  25.     }  
  26. }  
Startup.cs
 
ASP.NET Core apps use a Startup class, which is named Startup by convention.
 
This file optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality. All the services will be registered in the startup file under ConfigureServices and will be consumed across the application via dependency injection.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Builder;  
  6. using Microsoft.AspNetCore.Hosting;  
  7. using Microsoft.Extensions.Configuration;  
  8. using Microsoft.Extensions.DependencyInjection;  
  9. using Microsoft.Extensions.Hosting;  
  10.   
  11. namespace MvcCoreFirstApplication  
  12. {  
  13.     public class Startup  
  14.     {  
  15.         public Startup(IConfiguration configuration)  
  16.         {  
  17.             Configuration = configuration;  
  18.         }  
  19.   
  20.         public IConfiguration Configuration { get; }  
  21.   
  22.         // This method gets called by the runtime. Use this method to add services to the container.  
  23.         public void ConfigureServices(IServiceCollection services)  
  24.         {  
  25.             services.AddControllersWithViews();  
  26.         }  
  27.   
  28.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  29.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  30.         {  
  31.             if (env.IsDevelopment())  
  32.             {  
  33.                 app.UseDeveloperExceptionPage();  
  34.             }  
  35.             else  
  36.             {  
  37.                 app.UseExceptionHandler("/Home/Error");  
  38.             }  
  39.             app.UseStaticFiles();  
  40.   
  41.             app.UseRouting();  
  42.   
  43.             app.UseAuthorization();  
  44.   
  45.             app.UseEndpoints(endpoints =>  
  46.             {  
  47.                 endpoints.MapControllerRoute(  
  48.                     name: "default",  
  49.                     pattern: "{controller=Home}/{action=Index}/{id?}");  
  50.             });  
  51.         }  
  52.     }  
  53. }