Foreword
The previous chapter provides Asp.Net Core of past lives, and created a console project to compile and run successfully, this chapter describes .NETCore variety of commonly used commands, Asp.Net Core MVC project file directory information, by learning and operation commands, the understanding of the structure of the project, a further understanding of the operating mechanism and the framework of the project Asp.Net Core.
Create a command of project
First, to understand what the various commands that can be used to create a project, .NETCore dotnet commands are to begin with, it is well understood, enter dotnet xxx, is the execution environment variable to point to the C: \ Program Files \ dotnet \ dotnet.exe program, then provide parameters to dotnet.exe run. Recommended for beginners to perform the following commands one by one again, enhance memory and understanding, can not remember if it does not matter, you can enter commands in the console
1. Help commands
dotnet --help
// 或者
dotnet optional --help
// 如
dotnet new --help // 了解创建项目的帮助文档
2. dotnet new project to create various types of
template | Short Name | Language |
---|---|---|
Console Application | console | [C#]、F#、VB |
Class Library | classlib | [C#]、F#、VB |
Unit Test Project | mstest | [C#]、F#、VB |
xUnit Test Project | xunit | [C#]、F#、VB |
Razor found Page | page | [C#] |
MVC ViewImports | viewimports | [C#] |
MVC ViewStart | viewstart | [C#] |
ASP.NET Core empty | web | [C#],F# |
ASP.NET Core Web Application (Model - View - Controller) | mvc | [C#],F# |
ASP.NET Core Web Application | razor | [C#] |
With the ASP.NET Core Angular | angular | [C#] |
With the ASP.NET Core React.js | react | [C#] |
React.js and Redux with the ASP.NET Core | reactredux | [C#] |
ASP.NET Core Web API | WebAPI | [C#],F# |
Razor library | razorclasslib | [C#] |
global.json file | globaljson | |
NuGet Configuration | nugetconfig | |
Web Configuration | webconfig | |
The solution file | sln |
- More commands, see
https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-new?tabs=netcore21
The most commonly used commands are four, these commands need to be performed in the project root directory
1. Create a asp.net core mvc project, specify the project name as MyMvc
dotnet new mvc -o MyMvc
2. Restore project references package
dotnet restore
3. Compile the project
dotnet build
4. Run the project
dotnet run
Asp.Net Core project structure Introduction
1. First create a Asp.Net Core MVC project using the above command
dotnet new mvc -o MyMvc
2. Create a success, get a new Asp.Net Core MVC project
Open the project folder into the project, to see a standard Asp.Net Core MVC project is structured as follows
Role description of each file and directory
1. The three-tier architecture directory
/Models
/ViewS
/Controllers
This is a Asp.Net MVC and MVC project the same three-tier architecture, including the models (Models), view (Views), controller (Controllers), is simply Models can be defined within view (Views) passed through html controllers entity object within the controller
2. obj directory
This directory is used to store configuration file staging items, including references to the project configuration instructions
3. Properties directory
This directory is used to store assembly information, running configuration files, documents and other internal resources, the directory at the beginning of creation, will create a default launchSettings.json, the file contains the information of some programs start
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:23090", "sslPort": 44351 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "MyMvc": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
4. do not need to understand too much, enter the command to start the MVC project directly in the project root directory
dotnet run
You will get the following window output
A focus on information console output
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
This information represents Asp.Net Core MVC project has been successfully run under the 5001/5000 port, open the connection address in the browser
https://localhost:5001
Figure launchSettings.json again look at the information in, find the information below, you will understand the role of this document is to run port configuration item information such as
"applicationUrl": "https://localhost:5001;http://localhost:5000",
5. wwwwroot
This directory contains html page static resource reference view layer (Views), such as images, style sheets, scripts (js), etc.
6. appsettings.json 和 appsettings.Development.json
项目主配置文件,两个文件格式完全相同,实际上 appsettings.Development.json 表示 开发时运行程序的配置文件,使用 appsettings.{env}.json,可以很方便进行运行环境的切换,以加载不同的配置文件
7. MyMvc.csproj 项目描述文件,用记事本打开它,看看
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> </ItemGroup> </Project>
第一行表示项目是一个 web 应用程序,当前使用的 SDK 版本为 netcoreapp2.1,配置节点 ItemGroup 包含了项目引用的 Nuget 包,如有新的包引用,可以加入该 ItemGroup 配置节
8.Program.cs 文件,程序运行主入口,Main 方法所在
9.Startup.cs
- 该文件是默认文件,不可随意删除,在此文件中可以以包含服务配置、定义请求处理管道的重要操作。
结语
总的来说,这是一篇小白入门答疑,简单的介绍了一个标准的 Asp.Net Core MVC 项目都包含了哪些文件和目录,以及各自的作用,希望对未入门的同学有点帮助