Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 433
diegosasw Avatar asked Nov 16 '25 08:11

diegosasw


2 Answers

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");
}
like image 92
Shyju Avatar answered Nov 19 '25 06:11

Shyju


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.

like image 36
AlignedDev Avatar answered Nov 19 '25 07:11

AlignedDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!