NET Core & VS Code Road (2) Web API
Conditions for developing core projects
- Visual Studio Update 3
- . NET Core 1.0.0-vs Tooling Preview 2
See the size of the VS package and don't want to go down.
Fortunately, MS is out of vs Code and can develop. NET core projects. Notepad can actually write core code, but development needs to be debugged.
So this series will continue to develop with VS code. Code-based, key points will be commented
Because of the company more, has not had time to update
Directory
Web API
- dotnet New Add Core project
- Project.json adding dependencies
- Add Startup.cs
- Add ValuesController.cs
- Modify Program.cs
Dependencies added by Project.json
| 1234 |
"dependencies": { "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", //webhost "Microsoft.AspNetCore.Mvc": "1.0.0"//webapi mvc} |
The added Startup.cs
| 123456789101112131415 |
usingMicrosoft.AspNetCore.Builder;usingMicrosoft.Extensions.DependencyInjection;publicclassStartup{ publicvoidConfigure(IApplicationBuilder app) { app.UseMvc();//使用MVC管道 } publicvoidConfigureServices(IServiceCollection services) { services.AddMvc();//MVC加入到DI容器中 }} |
The added ValuesController.cs
| 1234567891011 |
usingMicrosoft.AspNetCore.Mvc;publicclassValuesController{ [HttpGet("/values/{name}")]//name参数注入 publicstringIndex(stringname) { return "Hello World:"+ name; }} |
Modified Program.cs
| 123456789 |
publicstaticvoidMain(string[] args){ new WebHostBuilder() .UseKestrel() .UseStartup<Startup>() .UseUrls("http://localhost:8899", "http://localhost:9988/")//同时监听2个端口 .Build() .Run();} |
Mvc
In. NET core, MVC and the Web API actually walk the same pipeline
This example will be modified based on the Web API above
- Adding and modifying Project.json
- Microsoft.AspNetCore.Razor.Tools
- Add HomeController
- Add index.cshtml
- Modify Program.cs
Because of the razor dynamic compilation in MVC, it's not just adding 1 dependencies
Project.json What to modify
| 12345678910111213 |
"buildOptions": { "debugType": "portable", "emitEntryPoint": true, "preserveCompilationContext": true//动态编译},"dependencies": { "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", //webhost "Microsoft.AspNetCore.Mvc": "1.0.0", //webapi mvc "Microsoft.AspNetCore.Razor.Tools": { //mvc razor "version": "1.0.0-preview2-final", "type": "build" }}, |
The added HomeController.cs
Note: The Controller class needs to be inherited
| 1234567891011 |
using Microsoft.AspNetCore.Mvc;publicclassHomeController : Controller{ [HttpGet("/{name}")] publicIActionResult Index(stringname) { ViewBag.Name = "Hello "+ name; returnView(); }} |
Add index.cshtml
The directory structure is the same as the previous way (ASP. NET MVC)
| 12345678910 |
<!DOCTYPE html>"zh"> <meta charset="UTF-8"> <title>Title</title><body> </body> |
Modify Program.cs
| 12345678910 |
publicstaticvoidMain(string[] args){ newWebHostBuilder() .UseKestrel() .UseStartup<Startup>() .UseContentRoot(Directory.GetCurrentDirectory()) //mvc views需要 .UseUrls("http://localhost:8899", "http://localhost:9988/")//同时监听2个端口 .Build() .Run();} |
static files
- Add Dependency: Project.json
- Microsoft.AspNetCore.StaticFiles
- Registration Pipeline: Startup.cs
- Add static file: Wwwroot
Project.json
| 123456789 |
"dependencies": { //nuget包 "Microsoft.AspNetCore.Mvc": "1.0.0", //webapi mvc "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", //webhost "Microsoft.AspNetCore.Razor.Tools": { //mvc razor "version": "1.0.0-preview2-final", "type": "build" }, "Microsoft.AspNetCore.StaticFiles": "1.0.0"//静态文件}, |
Register pipeline
| 12345 |
publicvoidConfigure(IApplicationBuilder app){ app.UseStaticFiles();//需要在mvc前 否则将优先被mvc路由匹配执行 app.UseMvc();} |
Add a static file
The static file defaults to the Wwwroot folder as the root directory
Attention:
- After you add a dependency, you need to restore before you can use the library
- This section removes a large number of descriptions of the configuration files due to space issues
- This address: http://www.cnblogs.com/neverc/p/5801210.html
NET Core & VS Code Road (2) Web API