ASP.NET Core - Project Layout



In this chapter, we will discuss how ASP.NET core project appears on the file system and how the different files and directories all work together.

Let us open the FirstAppDemo project created in the previous chapter.

First Demo App

In the Solution Explorer window, right-click on the Solution node and select the Open Folder in File Explorer.

Right Click on Solution

You will see now the root directory with two files in it: FirstAppDemo.sln and global.json.

Global Json

FirstAppDemo.sln is a solution file. Visual Studio has used this extension for years by default, and you can double-click on the file if you want to open the app in Studio and work on it.

There is also a global.json file. Let us open this file in Visual Studio.

Firstappdemo

In the file, the project's setting is significant. This project setting tells ASP.NET where to look for your source code and what folders contain your projects.

There are two possible folders “src” for source and a “test” folder. Unless your projects and source code are in one of these two folders, the code won't be available to build. You can change these settings if you want.

The Windows Explorer has the “src” folder on disk. You don't have a test folder. In the test folder, you could place your unit testing projects. Let us double-click on the “src” folder.

SRC Folder

You can see the FirstAppDemo project, and the web application. Now, double-click on the folder.

Web Application

These are the source code files for the application and you can also see this folder structure in the Solution Explorer window. This is because in the present version of ASP.NET Core, the file system determines what is in your project.

If you add a new file to the disk, the file will be added to the project. If you delete a file, the file is removed from the project. Everything stays in sync and that is a little bit different than previous versions of ASP.NET Core where a project file, a *.cs proj file, that contained a manifest of everything that is in the project.

ASP.NET Core also compiles your application when a file changes or a new file appears.

Example

Let us see a simple example by opening the Startup.cs file in the text editor.

Example

It is this line of code that responds to every HTTP request to your application and it simply responds with Hello World!

Let us change the string in the above screenshot by saying “Hello World! This ASP.NET Core Application” as shown in the following program.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 

using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.Http; 

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;  

namespace FirstAppDemo {
   public class Startup { 
      // This method gets called by the runtime. 
      // Use this method to add services to the container. 
      // For more information on how to configure your application, 
      // visit http://go.microsoft.com/fwlink/?LinkID=398940 
      public void ConfigureServices(IServiceCollection services) { 
      }  
      
      // This method gets called by the runtime. 
      // Use this method to configure the HTTP request pipeline. 
      public void Configure(IApplicationBuilder app, 
         IHostingEnvironment env, ILoggerFactory loggerFactory) { 
         loggerFactory.AddConsole();  
         
         if (env.IsDevelopment()){ 
            app.UseDeveloperExceptionPage(); 
         }  
         app.Run(async (context) => { 
            await context.Response.WriteAsync(
               "Hello World! This ASP.NET Core Application");
         });
      } 
   } 
} 

Save this file in the text editor by pressing Ctrl + S and then go back to the web browser and refresh the application.

Save and Open Browser

You can now see that your changes are reflected in the browser.

  • This is because ASP.NET will monitor the file system and automatically recompile the application when a file changes. You don't need to explicitly build the application in Visual Studio.

  • In fact, you can use a completely different editor, something like Visual Studio Code.

  • All you need to do with the Visual Studio is get the web server started by running without the debugger. You can also press Ctrl + F5, and can edit files, save files, and just refresh the browser to see the changes.

  • This is a nice workflow for building web applications with a compiled language like C#.

Advertisements