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

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

  • 相关阅读:
    kafka副本
    kafka消息丢失
    kafka消费者
    RN8302b调试笔记
    MDK #pragma anon_unions
    [Python笔记]Strip
    [Python笔记]AnyAll
    [Python笔记]元组
    [Python笔记]列表
    嵌入式平台移植Python
  • 原文地址:https://www.cnblogs.com/gamecc666/p/12971035.html
Copyright © 2020-2023  润新知