问题描述
我正在查看此链接,其中显示了如何从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请求的详细信息,例如标头,动词等.
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"
有人尝试过这个吗?
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
对象获取标头信息.
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()
方法.
[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
标头,该标头指向以250作为id参数值的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!