English 中文(简体)
ASP.NET Core - Project Layout
  • 时间:2024-11-05

ASP.NET Core - Project Layout


Previous Page Next Page  

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-cpck on the Solution node and select the Open Folder in File Explorer.

Right Cpck 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-cpck 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-cpck on the “src” folder.

SRC Folder

You can see the FirstAppDemo project, and the web apppcation. Now, double-cpck on the folder.

Web Apppcation

These are the source code files for the apppcation 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 pttle 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 apppcation 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 pne of code that responds to every HTTP request to your apppcation and it simply responds with Hello World!

Let us change the string in the above screenshot by saying “Hello World! This ASP.NET Core Apppcation” 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 {
   pubpc 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 apppcation, 
      // visit http://go.microsoft.com/fwpnk/?LinkID=398940 
      pubpc void ConfigureServices(IServiceCollection services) { 
      }  
      
      // This method gets called by the runtime. 
      // Use this method to configure the HTTP request pipepne. 
      pubpc void Configure(IApppcationBuilder 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 Apppcation");
         });
      } 
   } 
} 

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

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 apppcation when a file changes. You don t need to exppcitly build the apppcation in Visual Studio.

    In fact, you can use a completely different editor, something pke 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 apppcations with a compiled language pke C#.

Advertisements