如何访问 MVC 6 控制器中的请求对象?

How To Access The Request Object in an MVC 6 Controller?

使用 Asp.Net 5 MVC 6 控制器我正在移植 Api 控制器,但似乎无法弄清楚如何在控制器的操作中访问请求对象。

控制器示例:

[Microsoft.AspNet.Authorization.Authorize(Roles = "Admin,User")]
    public class DataManagementController
    {
        [Microsoft.AspNet.Mvc.HttpPost]
        public async Task<HttpResponseMessage> Prospects()
        {
           if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
        }
    }

上面的代码Request in the if condition isnot found and VS resolve 只提供了使用Microsoft.Net.Http.Server

的测试版

我是不是遗漏了什么明显的东西?

你需要继承自Controller;

using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using System.Net;
using System.Net.Http;

[Authorize(Roles = "Admin,User")]
public class DataManagementController : Controller // inherit
{
    [HttpPost]
    public HttpResponseMessage Prospects()
    {
        if (!HttpContext.Request.HasFormContentType)
            return new HttpResponseMessage(HttpStatusCode.BadRequest);

        return new HttpResponseMessage(HttpStatusCode.OK);
    }
}