.Net Core WebApi 基础

0、Startup.cs

服务和配置在此注入

1、控制器路由

[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase

[controller] 表示直接用当前控制器的名字,去掉中括号表示用指定名称如:[Route("api/student")]

2、action路由

[Route("GetPage")]
public ActionResult<string> GetPage(int page,int size)

在action 中直接指定名称用关键字 Route

3、方法路由

[HttpGet("[action]")] 也可以用这种方法的关键字 中括号代表用默认的action名称,{id} 表示 可以用任意字符响应,如action/1231,也可以指定响应类型如{id:int} 。不带括号表示指定action名称

4、参数匹配

[HttpPost]
public string PostStudent(StudentEntity model)

可以直接用json对象来填充

5、[FromForm]

表示用表单格式来填充

6、[FromBody]

比用用双引号的JSON来传递参数如:"{\"name\":\"1234\"}"

猜你喜欢

转载自www.cnblogs.com/liaoyi/p/12144270.html