【问题标题】:Alternative for HttpActionContext.RequestContentKeyValueModel on ASP.NET Web APIASP.NET Web API 上 HttpActionContext.RequestContentKeyValueModel 的替代方案
【发布时间】:2012-06-07 16:41:13
【问题描述】:

从 ASP.NET Web API 测试版开始,我使用HttpActionContext.RequestContentKeyValueModel 从 POST 请求的正文中获取输入参数:

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

    base.OnActionExecuting(actionContext);
}

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

【问题讨论】:

    标签: c# asp.net asp.net-web-api


    【解决方案1】:

    您可以使用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];
        }
    }
    

    在您的控制器中注入 IKeyValueProvider 并在您的测试中进行模拟。

    【讨论】:

    • 感谢您的回答,是的,它对我有用,但是,使用这种方法我们无法进行单元测试,我希望这种支持来自 Web Api。
    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多