ASP. NET Core Tutorial (iv)-Project structure

Source: Internet
Author: User

ASP. NET Core-Project structure
    1. ASP. NET Core-Project structure
    2. Case

In this chapter, we will discuss how the ASP. NET Core project is composed on a 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 been using the SLN extension name by default for many years, and if you want to open the application in Visual Studio, you can double-click the file.

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

In the Global.json file, the setup of the project is very important. This project setting tells ASP where to find your source code and which folders contain your project source code.

The newly created project contains two important folders: the source folder containing the sources 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.

We do not have the test folder in our current project. In the test folder, you can store items for your unit tests. Let's double-click the "src" folder.

You can see the Firstappdemo Web Application project now, double-click the folder.

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

If you add a new file to the project folder, the file is automatically added to the project. If you delete a file, the file is also deleted from the project. The project is kept in sync with the file system, which is a bit different from the previous version of ASP.

When a file changes or a new file is added, ASP. NET core will also automatically compile your application.

Case

Let's take a look at a simple example and open the Startup.cs file in Notepad:

The following line of code responds to every HTTP request made to the application, where it responds only to "Hello world!" ”

Let's change the string in the above and change it to "Hello world! This ASP. NET Core application, as shown below:

12345678910111213141516171819202122232425262728293031323334 using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Threading.Tasks; usingMicrosoft.AspNetCore.Builder; usingMicrosoft.AspNetCore.Hosting; usingMicrosoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Logging;  namespaceFirstAppDemo {   publicclassStartup {       // 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       publicvoidConfigureServices(IServiceCollection services) {       }              // This method gets called by the runtime.       // Use this method to configure the HTTP request pipeline.       publicvoidConfigure(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 the file, then go back to the Web browser and refresh the application.

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

    • This is because ASP. NET monitors the file system and automatically compiles the application when the file changes. You do not need to explicitly recompile an app in Visual Studio.

    • In fact, you can completely use a different editor, like visual Studio code.

    • All you need to do with Visual Studio is to 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 good process for building Web applications using C #.

ASP. NET Core Tutorial (iv)-Project structure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.