.NetCore MVC中控制器的返回类型

 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()

复制代码
 [HttpGet]
         public IHttpActionResult GetBadRequest(ORDER order)
         {
             if (string.IsNullOrEmpty(order.ID))
                 return BadRequest();
             return Ok();
         }
复制代码

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

  • Redirect(string location)

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

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

posted @ 2020-05-27 10:34  gamecc666  阅读(790)  评论(0编辑  收藏  举报