如何避开.net Core中的HttpResponseMessage(Request.CreateResponse) [英] How to get around HttpResponseMessage (Request.CreateResponse) in .net Core

查看:534
本文介绍了如何避开.net Core中的HttpResponseMessage(Request.CreateResponse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected HttpResponseMessage CreatedResponse(string routeName, object routeValues)
{
    var response = Request.CreateResponse(HttpStatusCode.Created);
    var locationUri = Url.Link(routeName, routeValues);
    response.Headers.Location = new Uri(locationUri);

    return response;
}

.net Core中的等效代码是什么? 或解决方法.

Whats the equivalent code in .net Core? Or a way around it..

谢谢

推荐答案

我在另一个答案的基础上,创建了一个扩展方法.您将必须返回IActionResult.因此,如果您仍然想以旧的方式进行操作,可以使用以下方法:

I built upon the other answer, and created an extension method. You will have to return an IActionResult. So if you still want to do it the old fashion way, you can with this:

public static class HttpRequestExtensions
{
  public static IActionResult CreateResponse(this HttpRequest request, int status, object content)
  {
    return new ObjectResult(content)
    {
      StatusCode = status
    };
  }
}

正在使用:

public IActionResult Patch(long id, [FromBody]JsonPatchDocument<SomeModel> value)
{
  //other code here for patching logic

  var response = new { Original = modelBefore, Patched = modelAfter };

  return Request.CreateResponse(StatusCodes.Status200OK, response);

}

这篇关于如何避开.net Core中的HttpResponseMessage(Request.CreateResponse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆