ASP.NET Web API上的HttpActionContext.RequestContentKeyValueModel的替代方法

cuo*_*gle 3 c# asp.net asp.net-web-api

从ASP.NET Web API的beta版本开始,我习惯HttpActionContext.RequestContentKeyValueModel从POST请求的主体获取输入参数:

public override void OnActionExecuting(HttpActionContext actionContext)
{
    var requestContentKeyValueModel = actionContext.RequestContentKeyValueModel;
    //Do something in here

    base.OnActionExecuting(actionContext);
}
Run Code Online (Sandbox Code Playgroud)

但是在新版本RC中,这个属性消失了,有什么替代方案吗?

Luk*_*Led 5

你可以用HttpContext.Current.Request.Form.

编辑

您始终可以将其隐藏在界面后面:

public interface IKeyValueProvider
{
    string GetValue(string key);
}

class RequestFormKeyValueProvider : IKeyValueProvider
{
    public string GetValue(string key)
    {
        return HttpContext.Current.Request.Form[key];
    }
}
Run Code Online (Sandbox Code Playgroud)

IKeyValueProvider在控制器中注入并在测试中进行模拟.