开发Core项目的条件
看到VS包的体积,以及不想往下走了.
幸亏,ms出了VS Code,可开发.NET Core项目.实际上记事本也能写Core代码,但是开发是需要调试的.
所以本系列将持续用VS Code开发.以代码为主,关键点会加以注释
由于公司事比较多,一直没来得及更新
目录
Web API
- dotnet new 添加Core项目
- project.json添加依赖
-
"Microsoft.AspNetCore.Server.Kestrel"
-
"Microsoft.AspNetCore.Mvc"
-
- 添加Startup.cs
- 添加ValuesController.cs
- 修改Program.cs
project.json 添加的依赖
"dependencies": { "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", //webhost "Microsoft.AspNetCore.Mvc": "1.0.0" //webapi mvc }
添加的Startup.cs
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; public class Startup { public void Configure(IApplicationBuilder app) { app.UseMvc();//使用MVC管道 } public void ConfigureServices(IServiceCollection services) { services.AddMvc();//MVC加入到DI容器中 } }
添加的ValuesController.cs
using Microsoft.AspNetCore.Mvc; public class ValuesController { [HttpGet("/values/{name}")]//name参数注入 public string Index(string name) { return "Hello World:" + name; } }
修改的Program.cs
public static void Main(string[] args) { new WebHostBuilder() .UseKestrel() .UseStartup<Startup>() .UseUrls("http://localhost:8899", "http://localhost:9988/")//同时监听2个端口 .Build() .Run(); }
MVC
在.NET Core中,MVC和Web API实际上走的同一个管道
本例子会在上面的Web API基础上修改
- 添加和修改project.json
- Microsoft.AspNetCore.Razor.Tools
- 添加HomeController
- 添加Index.cshtml
- 修改Program.cs
MVC中因为有razor动态编译,所以不仅仅添加1个依赖即可
project.json修改的内容
"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" } },
添加的HomeController.cs
注意:需要继承Controller类
using Microsoft.AspNetCore.Mvc; public class HomeController : Controller { [HttpGet("/{name}")] public IActionResult Index(string name) { ViewBag.Name = "Hello " + name; return View(); } }
添加Index.cshtml
目录结构和之前方式(ASP.NET MVC)一样
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>@ViewBag.Name</h1> </body> </html>
修改Program.cs
public static void Main(string[] args) { new WebHostBuilder() .UseKestrel() .UseStartup<Startup>() .UseContentRoot(Directory.GetCurrentDirectory()) //mvc views需要 .UseUrls("http://localhost:8899", "http://localhost:9988/")//同时监听2个端口 .Build() .Run(); }
静态文件
- 添加依赖:project.json
- Microsoft.AspNetCore.StaticFiles
- 注册管道:Startup.cs
- 添加静态文件:wwwroot
project.json
"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"//静态文件 },
注册管道
public void Configure(IApplicationBuilder app) { app.UseStaticFiles();//需要在mvc前 否则将优先被mvc路由匹配执行 app.UseMvc(); }
添加静态文件
静态文件默认是以wwwroot文件夹为根目录
注意:
- 添加依赖后,需要Restore后才能使用该库
- 本节由于篇幅问题,去除了配置文件的大量说明
- 本文地址:http://www.cnblogs.com/neverc/p/5801210.html