Home >Backend Development >C#.Net Tutorial >Project structure of asp.net core example tutorial

Project structure of asp.net core example tutorial

零下一度
零下一度Original
2017-06-16 09:58:192061browse

Asp.Net Core-Project Structure

  1. Asp.Net Core-Project Structure

  2. Case

In this chapter, we will discuss how ASP.NET Core projects are organized on the file system and how different files and directories work together.

Let's open the FirstAppDemo project created in the previous chapter.

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

You will see two files in its root directory: FirstAppDemo.sln and global.json.

The FirstAppDemo.sln file is a solution file. Visual Studio has used the sln extension by default for many years. If you want to open the application in Visual Studio, you can double-click this file.

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

In the global.json file, the project settings are very important. This project setting tells ASP.NET where to look for your source code and which folders contain your project source code.

Generally, a newly created project contains two important folders: the "source" folder containing the source code and a "test" folder. Unless your project and source code are in both folders, the project will fail to compile. If necessary, you can change these settings to suit your needs.

There is no test folder in our current project. In the test folder, you can store your unit test projects. Let's double click on the "src" folder.

You can see the FirstAppDemo web application project now, double click on the folder.

These are the source code files for the application, you can also see this folder structure in the Solution Explorer window.

If you add a new file to the project folder, the file will automatically be added to the project. If you delete a file, the file will also be deleted from the project. Everything stays in sync between the project and the file system, which is a bit different from previous Asp.NET versions.

ASP.NET Core will also automatically compile your application when files change or new files are added.


Case

Let us look at a simple example, open the Startup.cs file in Notepad:

The following line of code is used to respond to every HTTP request made to the application. Here it only responds with "Hello World!"

Let's modify the string in the screenshot above to "Hello World ! This ASP.NET Core Application", as shown below:

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");
         });
      } 
   } 
}

Press Ctrl+S in the text editor to save this file, then go back to the web browser and refresh the application.

You can now see your changes reflected in the browser.

  • This is because ASP.NET monitors the file system and automatically compiles the application when files change. You don't need to explicitly recompile the app in Visual Studio.

  • Actually, you can use a different editor, such as Visual Studio Code, etc.

  • All you need to do when using Visual Studio is start the web server by running the debugger. You can also press Ctrl + F5 to edit the file, save the file, and refresh the browser to see the changes.

  • This is a great flow for building web applications using C#.

The above is the detailed content of Project structure of asp.net core example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn