WebApiCompatShim - 如何使用 MVC 6 配置 REST api [英] WebApiCompatShim - how to configure for a REST api with MVC 6

查看:20
本文介绍了WebApiCompatShim - 如何使用 MVC 6 配置 REST api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 此链接显示如何从 Web API 2 迁移到 MVC 6.

I was having a look at this link that shows how to migrate from Web API 2 to MVC 6.

我试图在绑定了 HttpRequestMessage 的控制器中使用 Action 方法.这适用于 Web Api 2.

I am trying to have Action methods in my controllers with the HttpRequestMessage bound. This works in Web Api 2.

 [Route("", Name = "AddTaskRoute")]        
 [HttpPost]        
 public Task    AddTask(HttpRequestMessage requestMessage, [FromBody]NewTask newTask)
 {            
      var task = _addTaskMaintenanceProcessor.AddTask(newTask); 
      return task;         
 } 

requestMessage 包含了 Http 请求的详细信息,例如 headers、verb 等.

and the requestMessage contains the details about the Http request such as headers, verb, etc.

我正在尝试使用 MVC 6 获得相同的结果,但 requestMessage 似乎绑定不正确,并且它显示了详细信息,例如当操作实际上是 POST 时方法是 GET.我相信我没有按照文章建议配置 WebApiCompatShim,因此绑定没有正确完成.但是我没有扩展方法 services.AddWebApiConventions(); 在版本 "Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-rc1-final"

I am trying to get the same with MVC 6 but the requestMessage seems to be incorrectly bound and it shows details such as the method being GET when the action is actually a POST. I believe I haven't configured the WebApiCompatShim as per the article suggests so the binding is not properly done. But I do not have the extension method services.AddWebApiConventions(); available in the version "Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-rc1-final"

有人试过这个成功了吗?

Anybody has succeed when trying this?

PS:我的控制器中可用的 Request 属性似乎包含有关 http 请求的详细信息,但我想要 HttpRequestMessage 实例.

PS: The Request property available in my controller seems to have details about the http request, but I'd like to have the HttpRequestMessage instance.

推荐答案

在MVC6中,你应该可以使用Request对象来获取header信息.

In MVC6, You should be able to use the Request object to get header information.

var contentTypeHeader = Request.Headers["Content-Type"];

确实,他们删除了一些不错的方法,例如 Request.CreateResponse()OK() 等.但是您可以使用一些替代方法.

It is true that they removed some of the nice methods like Request.CreateResponse() and OK() etc.. But there are some alternatives you can use.

我们将用来创建响应的所有这些类都继承自 ObjectResult 基类.所以你可以使用 ObjectResult 作为你的 Web api 方法的返回类型.

All of these classes we will be using to create a response are inheriting from the ObjectResult base class. So you can use ObjectResult as the return type of your Web api method.

在 MVC6 中,您可以使用创建 HttpOKObjectResult 类的对象并将其用作返回值,而不是 Request.CreateResponse().这将为响应生成状态代码 200 OK.

In MVC6, You can use create an object of HttpOKObjectResult class and use that as your return value instead of Request.CreateResponse(). This will produce the status code 200 OK for the response.

Web API2 代码

public HttpResponseMessage Post([FromBody]string value)
{
    var item = new { Name= "test", id = 1 };
    return Request.CreateResponse(HttpStatusCode.OK,item);
}

MVC 6 代码

[HttpPost]
public ObjectResult Post([FromBody]string value)
{
    var item = new {Name= "test", id=1};
    return new HttpOkObjectResult(item);
}

或者干脆使用 OK() 方法.

Or simply use the OK() method.

[HttpPost]
public ObjectResult Post([FromBody]string value)
{
    var item = new {Name= "test", id=1};
    return Ok(item);
}

CreatedAtRouteResult

您可以使用 CreatedAtRouteResult 类发送带有 201 Created 状态码和位置标头的响应.

CreatedAtRouteResult

You can use CreatedAtRouteResult class to send a response with 201 Created status code with a location header.

MVC 6 代码

[HttpPost]
public ObjectResult Post([FromBody]string value)
{
    var item = new { Name= "test", id=250};
    return new CreatedAtRouteResult(new { id = 250}, item);
}

客户端将在响应中收到一个 location 标头,该标头将指向 id 参数值为 250 的 api 路由.

The client will receive a location header in the response which will point to the api route with 250 as the value for the id parameter.

您可以使用该类返回 404 Not found 响应.

You can use this class to return a 404 Not found response.

Web API2 代码

public HttpResponseMessage Post([FromBody]string value)
{
    return Request.CreateResponse(HttpStatusCode.NotFound);   
}

MVC 6 代码

[HttpPost]
public ObjectResult Post([FromBody]string value)
{
    return new HttpNotFoundObjectResult("Some");
}

这篇关于WebApiCompatShim - 如何使用 MVC 6 配置 REST api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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