1.返回 200 状态码(200 - Ok)
在Models文件夹下创建一个AnimalModel.cs脚本,代码如下
using System;
namespace MyWebApi.Models
{
public class AnimalModel
{
public int Id {
get; set; }
public string Name {
get; set; }
}
}
新建一个Animals控制器,AnimalsController.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 AnimalsController : ControllerBase
{
public IActionResult GetAnimals()
{
return Ok("all animals");
}
}
}
运行结果:
OK方法里也可以直接传入一个对象,修改AnimalsController.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 AnimalsController : ControllerBase
{
public IActionResult GetAnimals()
{
List<AnimalModel> animals = new List<AnimalModel>() {
new AnimalModel {
Id = 1, Name = "Brid" },
new AnimalModel {
Id = 2, Name = "Tigger" }};
return Ok(animals);
}
}
}
运行结果:
2.返回 202 状态码(202 - accepted)
AnimalsController.cs脚本中添加GetAnimalsAccepted()方法,代码如下
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 AnimalsController : ControllerBase
{
public IActionResult GetAnimals()
{
List<AnimalModel> animals = new List<AnimalModel