Project Layout In ASP.NET Core 1.0

Introduction

ASP.NET Core 1.0 was previously called ASP.NET 5. It was renamed in January 2016. It supports cross-platform frameworks ( Windows, Linux, Mac ) for building modern cloud-based internet-connected applications like IOT, web apps, and mobile back-end. In my previous article, I explained Installation and Printing Hello World in Asp.Net Core 1.0. In this article, I will explain the Project Layout in ASP.NET Core 1.0.

Importance Of Asp.Net Core 1.0

  • Packages Overview
    NuGet ( Server Side ) & Bower ( Client Side ) & Node.Js ( Build Tools and Node.js )
  • Removed duplication and now MVC and Web API are the features of ASP.Net Core.
  • Modular libraries & runtime optimized for the server and cloud workloads.
  • Cross platform support (build & run on Windows, Linux and MAC).
  • ASP.NET Core (previously known as ASP.NET 5) is open source.
  • Performance improvement and much faster than the old versions.
  • Ability to host on IIS or self-host in your own process.
  • Unified framework for MVC, Web API and SignalR.
  • Seamless transition from on-premises to cloud (Full cloud-based support)
  • Full support of NuGet Package Manager.
  • Automatically recompile the application.
  • Built-in dependency injection.
  • Light weight and fast development cycle.
  • Rich framework and easy to maintain.

ASP.NET Core Architecture

The architecture given below clearly mentions that .NET Core framework is cross platform support (build & run on Windows, Linux and MAC).
 


Project Layout Structure
 

The image given below represents the project structure of the ASP.NET Core 1.0 empty template .
 
Project Layout Structure for Asp.Net Core 1.0 

The Important files/folders in ASP.NET Core 1.0

  • global.json
  • wwwroot
  • Dependencies
  • Program.cs
  • project.json
  • Startup.cs
  • appsettings.json
  • Web.config

global.json

The default templates generate the schema given below.
  1. {  
  2.   "projects": [ "src""test" ],  
  3.   "sdk": {  
  4.     "version""1.0.0-preview2-003131"  
  5.   }  
  6. }  
The projects setting helps to show our ASP.NET project source code and it shows what folder contains our project, or we can simply say it  finds the root of the projects and “sdk” defines the version of dnx.exe from one of the runtimes which you installed, or it contains the version of the tool.

wwwroot 

The new folder in ASP.NET Core is wwwroot and it stores all the static files in our project. The static files means that the HTML files, CSS files, image files, and JavaScript files which are sent to the users' browsers should be stored inside the wwwroot folder. 

Dependencies 

We can add or remove project dependencies.

Program.cs 

The entry point of an ASP.NET Core 1.0.
  1. using System.IO;  
  2. using Microsoft.AspNetCore.Hosting;  
  3.    
  4. namespace HelloWorldDotnetCore  
  5. {  
  6.     public class Program  
  7.     {  
  8.         public static void Main(string[] args)  
  9.         {  
  10.             var host = new WebHostBuilder()  
  11.                 .UseKestrel()  
  12.                 .UseContentRoot(Directory.GetCurrentDirectory())  
  13.                 .UseIISIntegration()  
  14.                 .UseStartup<Startup>()  
  15.                 .Build();  
  16.    
  17.             host.Run();  
  18.         }  
  19.     }  
  20. }  

WebHostBuilder()

The builder pattern of ASP.NET Core and creates a web application host.

UseKestrel() 

We are all familiar with the Web Browser Internet Information Service (IIS) in ASP.NET. In cross-platform, the Web Server for ASP.NET Core is Kestrel and it is supported on all the platforms and versions that .NET Core supports. 

UseContentRoot() 

UseContentRoot is required for specifying the root content directory for hosting in IIS and IIS Express. 
Example : 
ContentRoot : D:\HelloWorldDotnetCore(Our App Name)\ 

UseWebRoot() 

It contains the Web-servable content files. In ASP.NET Core Web.Config is besides the WebRoot. 

Example

WebRoot: D:\HelloWorldDotnetCore(Our App Name)\wwwroot\ 

UseIISIntegration() 

Why are we using two web servers in a code? Since it uses UseKestrel and UseIISIntegration, which are different in ASP.NET Core, they both are created with different actions.
 
UseIISIntegration IIS is only used as a reverse proxy in ASP.NET Core. UseKestrel creates the Web Server and hosts the code.

UseStartup<Startup>() 

The Startup class is helpful to define the request handling pipeline and configure the services required by the app. We can declare our startup class name inside the “<>”

Build & Run 

Both methods build the IWebHost, which will host the app and start it wait for incoming HTTP requests. 

project.json 

In this project.json, file contains the version information and details of ASP.NET Core framework, tools, build, run and publish options, scripts and dependencies of all the files, like front-end etc.  It clearly mentions that it is the replacement of Web.Config & Pacage.Config file.
  1. {  
  2.   "dependencies": {  
  3.     "Microsoft.NETCore.App": {  
  4.       "version""1.0.1",  
  5.       "type""platform"  
  6.     },  
  7.     "Microsoft.AspNetCore.Diagnostics""1.0.0",  
  8.     "Microsoft.AspNetCore.Server.IISIntegration""1.0.0",  
  9.     "Microsoft.AspNetCore.Server.Kestrel""1.0.1",  
  10.     "Microsoft.Extensions.Logging.Console""1.0.0",  
  11.     "Microsoft.AspNetCore.Mvc""1.0.1"  
  12.   },  
  13.    
  14.   "tools": {  
  15.     "Microsoft.AspNetCore.Server.IISIntegration.Tools""1.0.0-preview2-final"  
  16.   },  
  17.    
  18.   "frameworks": {  
  19.     "netcoreapp1.0": {  
  20.       "imports": [  
  21.         "dotnet5.6",  
  22.         "portable-net45+win8"  
  23.       ]  
  24.     }  
  25.   },  
  26.    
  27.   "buildOptions": {  
  28.     "emitEntryPoint"true,  
  29.     "preserveCompilationContext"true  
  30.   },  
  31.    
  32.   "runtimeOptions": {  
  33.     "configProperties": {  
  34.       "System.GC.Server"true  
  35.     }  
  36.   },  
  37.    
  38.   "publishOptions": {  
  39.     "include": [  
  40.       "wwwroot",  
  41.       "web.config"  
  42.     ]  
  43.   },  
  44.    
  45.   "scripts": {  
  46.     "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]  
  47.   }  
  48. }  

Startup.cs

The startup class is helpful to define the request handling pipeline and configure the Services required by the app. The code given below is the one where we added services.AddMvc() method. With the help of NuGet Package Manager, we can install all the dependencies files for MVC. 
  1. using Microsoft.AspNetCore.Builder;  
  2. using Microsoft.AspNetCore.Hosting;  
  3. using Microsoft.AspNetCore.Http;  
  4. using Microsoft.Extensions.DependencyInjection;  
  5. using Microsoft.Extensions.Logging;  
  6.    
  7. namespace HelloWorldDotnetCore  
  8. {  
  9.     public class Startup  
  10.     {  
  11.         public Startup(IHostingEnvironment env)  
  12.         {  
  13.    
  14.         }  
  15.         // This method gets called by the runtime. Use this method to add services to the container.  
  16.         // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940  
  17.         public void ConfigureServices(IServiceCollection services)  
  18.         {  
  19.             services.AddMvc();//Mvc related files  
  20.         }  
  21.    
  22.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  23.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  24.         {  
  25.             loggerFactory.AddConsole();  
  26.    
  27.             if (env.IsDevelopment())  
  28.             {  
  29.                 app.UseDeveloperExceptionPage();  
  30.             }  
  31.    
  32.             app.Run(async (context) =>  
  33.             {  
  34.                 await context.Response.WriteAsync(" Welcome to Dotnet Core !!");  
  35.             });  
  36.    
  37.    
  38.         }  
  39.     }  
  40. }  

IApplicationBuilder

In Middleware, the request delegates are used to build the request pipeline or it handles each HTTP request. IApplicationBuilder provides all the important extension methods (Use, run and map ) for configuring request delegates.

IHostingEnvironment 

It provides the information about the Web hosting environment.

ILoggerFactory  

It provides the log factory information or records the log information from each middleware request pipline in our project.

appsettings.json 

This file is not available in an empty template. Thus, we can add through “Ctrl + Shift + A ” or “Add -> New Item – Type JSON in search area -> Select ASP.NET Configuration File “
 
JSON file given below contains the information about “ConnectionStrings”.
  1. {  
  2.   "ConnectionStrings": {  
  3.     "DefaultConnection""Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"  
  4.   }  
  5. }  

Web.config

In the previous versions, everything is handled by Web.Config but in ASP.NET Core, we don't know about Web configure file and the only purpose it gives the instruction to iis for what should be done and it receives the http request.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.    
  4.   <!-- Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380 -->  
  5.    
  6.   <system.webServer>  
  7.     <handlers>  
  8.       <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>  
  9.     </handlers>  
  10.     <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>  
  11.   </system.webServer>  
  12. </configuration>  

Reference

Conclusion

We learned project layout In ASP.NET Core 1.0 and I hope you liked this article. Please share your valuable suggestions and feedback.