1.RESTful URLs
RESTful URLs是使用Http动词和URL定义的。
例:
GET + http//example.com/Employees
1.1 RESTful Get URLs
GET + domain.com/employees
获取所有employees的清单
GET + domain.com/employees/{employeeId}
获取单个employee的详情
GET + domain.com/employees/{employeeId} /accounts
获取单个employee的账户详情
GET + domain.com/Employees/{employeeId} /accounts/{accountID}
获取单个employee的单个账户详情
1.2 RESTful Post URLs
Post + domain.com/employees
Body:Account object
Add a new account for the employee
Post + domain.com/employees/{employeeId}/accounts
Body:Account object
Add a new account for the employee
1.3 RESTful Put URLs
Put + domain.com/employees/{employeeId}
Body:Account object
更新 employee
Post + domain.com/employees/{employeeId}/accounts/{accountId}
Body:Account object
更新特定员工的特定账户
1.4 RESTful Delete URLs
Delete + domain.com/employees/{employeeId}
删除 employee
Post + domain.com/employees/{employeeId}/accounts/{accountId}
删除特定员工的特定账户
2.Action method return types
2.1 特定类型(Specific type)
也就是在事件方法中返回一个特定类型。
按如下层级创建一个Models文件夹,在创建一个EmployeeModel.cs脚本
EmployeeModel.cs脚本代码如下
using System;
namespace MyWebApi.Models
{
public class EmployeeModel
{
public int id { get; set; }
public string name { get; set; }
}
}
2.1.1对象形式
创建一个Employee控制器,EmployeeController.cs脚本代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;
namespace MyWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
public EmployeeModel GetEmployees()
{
return new EmployeeModel() { id = 13,name = "Lice"};
}
}
}
运行结果:
2.1.2 列表形式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;
namespace MyWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
public List<EmployeeModel> GetEmployees()
{
return new List<EmployeeModel>()
{ new EmployeeModel() { id = 13, name = "Lice" },
new EmployeeModel() { id = 18, name = "Mick" }};
}
}
}
运行结果:
2.1.3 迭代形式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;
namespace MyWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
public IEnumerable<EmployeeModel> GetEmployees()
{
return new List<EmployeeModel>()
{ new EmployeeModel() { id = 13, name = "Lice" },
new EmployeeModel() { id = 17, name = "Mick" }};
}
}
}
运行结果:
2.1.4 异步迭代器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;
namespace MyWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
public async IAsyncEnumerable<EmployeeModel> GetEmployees()
{
yield return new EmployeeModel() { id = 17, name = "Mick" };
await Task.Delay(1);
}c
}
}
运行结果:
2.2 IActionResult
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;
namespace MyWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
[Route("")]
public async IAsyncEnumerable<EmployeeModel> GetEmployees()
{
yield return new EmployeeModel() { id = 17, name = "Mick" };
await Task.Delay(1);
}
[Route("{id}")]
public IActionResult GetEmployees(int id)
{
if(id == 0)
{
return NotFound();
}
return Ok(
new List<EmployeeModel>()
{
new EmployeeModel() { id = 1, name = "Lice" },
new EmployeeModel() { id = 2, name = "Mick" }
});
}
}
}
运行结果:
id = 0
id = 1
可以看到id = 0 时到状态码为404,id = 1时到状态码为200
2.3 带类型ActionResult(ActionResult< T > )
[Route("{id}/base")]
public ActionResult<EmployeeModel> GetEmployeesBase(int id)
{
if (id == 0)
{
return NotFound();
}
return Ok(new EmployeeModel() { id = 1, name = "Lice" });
}
运行结果:
如图所示请求成功状态码为200
下一节我门就来讨论一下状态码: