【问题标题】:In ASP.Net Core MVC, what's "value" used for in ControllerBase.StatusCode()?在 ASP.Net Core MVC 中,ControllerBase.StatusCode() 中使用的“价值”是什么?
【发布时间】:2019-06-26 19:26:54
【问题描述】:

这是第二个参数。我没有看到任何描述当我返回具有该参数集的 StatusCode ObjectResult 时会发生什么的文档。

    //
    // Summary:
    //     Creates a Microsoft.AspNetCore.Mvc.ObjectResult object by specifying a statusCode
    //     and value
    //
    // Parameters:
    //   statusCode:
    //     The status code to set on the response.
    //
    //   value:
    //     The value to set on the Microsoft.AspNetCore.Mvc.ObjectResult.
    //
    // Returns:
    //     The created Microsoft.AspNetCore.Mvc.ObjectResult object for the response.
    [NonAction]
    public virtual ObjectResult StatusCode(int statusCode, object value);

【问题讨论】:

    标签: asp.net asp.net-core asp.net-core-mvc


    【解决方案1】:

    value 将是响应中包含的有效负载/正文,由适用的媒体格式化程序格式化。

    对于下面的代码以及使用 application/json 内容类型时,这将是

    { "a" : "foo", "b" : 1 }
    

    public class Dto
    {
        public string A { get; set; }
        public int B { get; set; }
    }
    
    
    public class MyController : Controller
    {
        [HttpGet]
        public IActionResult MyAction()
        {
            var dto = new Dto { A = "foo", B = 1};
            return StatusCode(200, dto);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-24
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      相关资源
      最近更新 更多