时间:2018年05月21日 | 作者 : aaronyang | 分类 : .NET CORE | 浏览: 1861次 | 评论 0 人
我的VS版本已经很高了,
在网上找了最新的.net core2.1 rc1,安装下
打开cmd, 输入dotnet
参考这个学习吧。
https://www.microsoft.com/net/learn/get-started/windows?sdk-installed=true
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
上篇文章我演示 2.1的 core 在centos环境部署有问题,此时我们直接在win下,当然学习最新的core了,从现在开始core技术使用SDK 2.1 RC,由于我只需要做服务端,所以关于core web的都不学习了。
新建
现在web几乎都是https,所以勾选了https。直接F5,看能不能运行
我浏览器装了json美化插件,所以这样显示的。
新建Pets控制器
下面是我AY把MSDN示例修改的,主要讲解一些使用,并无实际用处 www.ayjs.net
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace AyCoreChap1.Controllers { [Produces("application/json")] [Route("api/[controller]")] public class PetsController : ControllerBase { private readonly PetsRepository _repository; public PetsController(PetsRepository repository) { _repository = repository; } [HttpGet] public ActionResult<List<Pet>> Get() { return _repository.GetPets(); } [HttpGet("{id}")] [ProducesResponseType(404)] public ActionResult<Pet> GetById(int id) { if (!_repository.TryGetPet(id, out var pet)) { return NotFound(); } return pet as Pet; } [HttpPost] [ProducesResponseType(400)] public async Task<ActionResult<Pet>> CreateAsync(Pet pet) { if (!ModelState.IsValid) { return BadRequest(ModelState); } await _repository.AddPetAsync(pet); return CreatedAtAction(nameof(GetById), new { id = pet.Id }, pet); } } public class PetsRepository { internal Task AddPetAsync(Pet pet) { throw new NotImplementedException(); } internal ActionResult<List<Pet>> GetPets() { throw new NotImplementedException(); } internal bool TryGetPet(int id, out object pet) { throw new NotImplementedException(); } } public class Pet { public string Id { get; internal set; } } }
大致也能看懂几个
继承ControllerBase类,父类提供了很多方法和属性。F12可以查看
比如BadRequest 400错误,CreatedAtAction 返回201错误
ModelState 提供model验证。
ASP.NET Core 2.1 提供了 ApiController把类变成一个Web API类
ApiController特性 通常与ControllerBase一起使用,然后就可以使用ControllerBase的一些方法和属性。比如NotFound和File。
你可以直接使用
[Route("api/[controller]")] [ApiController] public class MyBaseController { [HttpGet] public ActionResult<IEnumerable<string>> Get() { return new string[] { "ay1", "ay2" }; } }
自动触发400错误,验证失败会自动触发400错误,下面写法就可以不必要了。
if (!ModelState.IsValid) { return BadRequest(ModelState); }
你可以Startup类下ConfigureServices方法增加下面代码,这些代码是默认,默认行为是禁用的。也就是不会触发400,也就上面说的返回BadRequest
MVC中Action具有参数,可以被下面修饰,从而绑定 值
不要使用FromRoute,如果你的URL可能具有%2f,可以用FromQuery
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
下面代码段,参数是 默认值类型的,说明FromQuery中可能没有discontinuedOnly参数,但是也可以使用
[HttpGet] public ActionResult<List<Product>> Get([FromQuery] bool discontinuedOnly = false) { List<Product> products = null; if (discontinuedOnly) { products = _repository.GetDiscontinuedProducts(); } else { products = _repository.GetProducts(); } return products; }
[FromBody]这个特性呢? 复杂参数设计的。什么是复杂呢,你理解为不是poco类型吧,就是类里封装了很多属性那种。
下面3个都是错误使用的
// Don't do this. All of the following actions result in an exception. [HttpPost] public IActionResult Action1(Product product, Order order) => null; [HttpPost] public IActionResult Action2(Product product, [FromBody] Order order) => null; [HttpPost] public IActionResult Action3([FromBody] Product product, [FromBody] Order order) => null;
[FromForm] 是从IFormFile和IFormFileCollection类型,推断出值,绑定的。
[FromRoute]从路由模板中的 参数名称 和 action 参数的名字 开始匹配,名字一样,就赋值了,当多个route匹配一个action的参数,任何路由值可以考虑fromroute
[FromQuery]做web都知道了,匹配 url的 ?AA=aadf&BB=cc啥的。 AA和BB都要匹配 Action的参数名称
还是Startup类下ConfigureServices方法,默认是禁用
options.SuppressInferBindingSourcesForParameters = true;
关于Multipart/form-data 的请求
当一个action的参数是被 [FromForm] 修饰了,那么 multipart/form-data 请求的content内容 就会被获得,然后你可以在action拿到
还是Startup类下ConfigureServices方法,默认是禁用
把一个类,变成一个可以接受web请求的 方法
通过使员工UseMvc() 就可以实现类似WebApi的 一些 规定,比如 controller的命名,mvc的一些套路。
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
推荐您阅读更多有关于“net core2,”的文章
抖音:wpfui 工作wpf,目前主maui
招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰
目前在合肥市企迈科技就职
AYUI8全源码 Github地址:前往获取
杨洋(AaronYang简称AY,安徽六安人)和AY交流
高中学历,2010年开始web开发,2015年1月17日开始学习WPF
声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费
不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo
查看捐赠AYUI7.X MVC教程 更新如下:
第一课 第二课 程序加密教程
额 本文暂时没人评论 来添加一个吧
发表评论