MSDN控制器基类地址:https://msdn.microsoft.com/zh-cn/library/system.web.mvc.actionresult.aspx?f=255&MSPPError=-2147217396

一:ActionResult 

 ActionResult 类是操作结果的基类。

ActionResult 派生:

二、IHttpActionResult

  • Json<T>(T content)

  return Json<List<ORDER>>(lstRes);

  • Ok()、 Ok<T>(T content)

  return Ok();

   return Ok<string>(name);

  • NotFound()

   return NotFound();

当需要向客户端返回找不到记录时,有时需要用到NotFound()方法

NotFound()方法会返回一个404的错误到客户端。

 

三:其他

  • Content<T>(HttpStatusCode statusCode, T value)

   [HttpGet]
        public IHttpActionResult GetContentResult()
         {
            return Content<string>(HttpStatusCode.OK, "OK");
        }

   向客户端返回值和http状态码。

  • BadRequest()

.NetCore MVC中控制器的返回类型
 [HttpGet]
         public IHttpActionResult GetBadRequest(ORDER order)
         {
             if (string.IsNullOrEmpty(order.ID))
                 return BadRequest();
             return Ok();
         }
.NetCore MVC中控制器的返回类型

  向客户端返回400的http错误。

  • Redirect(string location)

   [HttpGet]
        public IHttpActionResult RedirectResult()
        {
            return Redirect("http://localhost:21528/api/Order/GetContentResult");
        }

  将请求重定向到其他地方。

相关文章:

  • 2021-10-06
  • 2021-11-14
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
  • 2022-01-02
  • 2022-02-11
猜你喜欢
  • 2022-12-23
  • 2021-07-26
  • 2021-10-11
  • 2021-10-18
  • 2021-07-26
  • 2021-11-07
相关资源
相似解决方案