WebApiCompatShim - how to configure for a REST api with MVC 6

十年热恋 提交于 2019-12-20 03:35:26

问题


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

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;         
 } 

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

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: The Request property available in my controller seems to have details about the http request, but I'd like to have the HttpRequestMessage instance.


回答1:


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

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

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.

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.

HttpOKObjectResult

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 code

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

MVC 6 code

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

Or simply use the OK() method.

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

CreatedAtRouteResult

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

MVC 6 code

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

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.

HttpNotFoundObjectResult

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

Web API2 code

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

MVC 6 code

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



回答2:


I found that to use the Microsoft.AspNetCore.Mvc.WebApiCompatShim, it should be services.AddMvc().AddWebApiConventions() see this example instead of services.AddWebApiConventions() as shown in the docs.

I'm putting in a feedback item on their docs.



来源:https://stackoverflow.com/questions/34540431/webapicompatshim-how-to-configure-for-a-rest-api-with-mvc-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!