.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");
        }

  將請求重定向到其他地方。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM