【问题标题】:What is the ASP.NET Core equivalent to HttpRequestMessage?与 HttpRequestMessage 等效的 ASP.NET Core 是什么?
【发布时间】:2018-01-16 00:39:06
【问题描述】:

我找到了一个blog post,它显示了如何将 POST 的 JSON 作为字符串接收。

我想知道在控制器的 REST Post 方法中执行与以下代码相同的操作的新本地方法是什么:

public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
    var jsonString = await request.Content.ReadAsStringAsync();

    // Do something with the string 

    return new HttpResponseMessage(HttpStatusCode.Created);
}

下面的另一个选项对我不起作用,我想是因为我没有在请求标头中使用 Content-Type: application/json (无法更改),我得到了 415 。

public HttpResponseMessage Post([FromBody]JToken jsonbody)
{
    // Process the jsonbody 

    return new HttpResponseMessage(HttpStatusCode.Created);
}

【问题讨论】:

    标签: c# json rest asp.net-core asp.net-core-webapi


    【解决方案1】:

    在 .Net Core 中,他们合并了 Web API 和 MVC,因此您可以像这样使用 IActionResult 或其派生词之一。

    public IActionResult Post([FromBody]JToken jsonbody)
    {
        // Process the jsonbody 
    
        return Created("", null);// pass the url and the object if you want to return them back or you could just leave the url empty and pass a null object
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多