Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for HttpActionContext.RequestContentKeyValueModel on ASP.NET Web API

Since from version beta of ASP.NET Web API, I have used HttpActionContext.RequestContentKeyValueModel to get input parameters from body of POST request:

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

    base.OnActionExecuting(actionContext);
}

but in the new release version RC, this property disappeared, is there any alternative for this?

like image 710
cuongle Avatar asked Jan 21 '26 23:01

cuongle


1 Answers

You can use HttpContext.Current.Request.Form.

EDIT

You can always hide it behind interface:

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

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

Inject IKeyValueProvider in your controllers and mock in your tests.

like image 81
LukLed Avatar answered Jan 23 '26 11:01

LukLed



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!