ASP. NET Core: CMD command line + notepad create Console program and Web Application, coreconsole
Today I read Scott's introduction to ASP. NET Core introduction video, found that using command line step by step to create a project, add Package, Restore, Build, Run the implementation method, it is easier for us to understand.. NET Core.
The following are the course notes for this lesson.
Course preparation:
C: |
Open drive C |
Md supermva |
Create a supermva folder |
Color |
Change the command line color to green (more powerful) |
Cd supermva |
Open the supermva folder |
Cls |
Clear command line |
Dir |
View File Directories |
Cd .. |
Back to parent directory |
Notepad |
Open notepad directly |
Cd \
|
Return to the following directory |
Tab |
Automatic completion |
F7 |
View History commands |
Exit |
Exit cmd |
1. View dotnet core information in CMD
2. Create a Console Program
1) the following steps enable the Console to output "Hello World", which is equivalent to creating a Console application in VS → restoring the Nuget package → Build → running
2) modify Program. cs in notepad, read the user input value, and then output:
Using System; namespace supermva {class Program {static void Main (string [] args) {string name; Console. WriteLine ("Hello World! "); Name = Console. ReadLine (); Console. WriteLine ($" Hello {name} "); // new syntax }}}
3) The preceding steps are as follows:
Console Program created successfully: The properties files of Program. cs and. csproj projects are generated:
The bin folder is generated after build:
Modify Program. cs to save and run the Program again in dotnet run:
3. Create a Web Application
In a web application, the user sends an HTTP request to the server, and the server returns the response to the user.
In the project file, you also need to use the AspnetCore function to process HTTP requests and reference the namespace corresponding to (using.
Modify the Console application created above to a Web application.
1) Add the Nuget package: Microsoft. AspnetCore
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp1.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspnetCore" Version="1.1.1" /> </ItemGroup></Project>
2) Add the Startup. cs class to receive requests and return data.
- > Notepad Startup. cs
- > Dotnet run
At this time, run the program and output "Hello World" on the Console ". This is because the program entry is the Main method and will not be executed in the Startup. cs class.
using System;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Builder;namespace moveToWeb{ public class Startup { public void Configure(IApplicationBuilder app) { app.Run(context=>{ return context.Response.WriteAsync("Hello Web"); }); } }}
3) modify the Main method in Pragram. cs.
WebHost: Host, where the Web site is hosted.
Builder: create and build
Kestrel: A web server for ASP. NET Core based on libuv. This is the definition of Kestrel on GitHub.
Therefore, the translation of this Code is: Use Kestrel to create a Web Host, direct the request to the Startup. cs class for processing, and then Build.
Using System; using Microsoft. aspNetCore. hosting; using moveToWeb; // remember to add reference namespace supermva {class Program {static void Main (string [] args) {var host = new WebHostBuilder (). useKestrel (). useStartup <Startup> (). build (); host. run ();}}}
Tip: Application started is opened in the browser. The "Hello Web" output in Startup. cs is displayed.
In this way, the Web Application is created.
4. Further improve Web Application
In the above application, when we modify Startup. "Hello Web" → "Hello ASP. NET Core. click "save" and refresh the browser repeatedly. The modified value is not output.
This is because WebHoster does not know that our source file has changed.
Now we add a Watcher tool to allow WebHoster to monitor source files in real time.Run again automaticallyNext, return the latest response information.
1) In supermva. csproj, we add the DotNet. Watcher tool. Note that the Version is: Version = "1.0.0 -*"
<ItemGroup> <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0-*" /> </ItemGroup>
- > Dotnet restore
- > Dotnet build
Cli: Command Line Interface
2) re-run the Web program. The changes made in Startup. cs are immediately reflected in the browser.
Although VS is not simple and convenient, you can use the command + notepad method to create a Console and Web application. You can view the functions and changes of each command step by step. During the read creation process, the CMD window prompts, read error messages, and then fixes them. This step-by-step process makes it easier for us to understand the essence.
"Slow is fast", ASP. NET Core, VS2017 Just Come On!
Problems:
1, Unable to load the service index for source https://api.nuget.org/v3/index.json
→ Can FQ, or modify the Restore command parameter to Cnblogs Nuget address:> dotnet restore-s https://nuget.cnblogs.com/v3/index.json
2. If an error is prompted in the command line, you can see where the error is. If you add a new Nuget package, Restore it again.
MVA course address:
Introduction to ASP. NET Core with Visual Studio 2017
Learn what ASP. NET Core is, how to get and install it, and how to create a very quick sample application.
Reference website:
1) MVA: http://mva.microsoft.com
2) Docs: https://docs.microsoft.com
3) Net: https://www.microsoft.com/net
4) KestrelHttpServer: https://github.com/aspnet/KestrelHttpServer/
4) Scoot Blog: http://www.hanselman.com/
5) Nuget: NuGet image online trial run
Address: blog I-shanghai