Magnus Johansson
Magnus Johansson

Reputation: 28325

Is there a way to handle form post data in a Web Api controller?

In ASP.NET MVC , one can access the form post data:

var thisData = Request.Form["this.data"];

Is it possible to achieve the same functionality in a Web API ApiController?

Upvotes: 27

Views: 37933

Answers (5)

Nick
Nick

Reputation: 3950

If you just have an HttpRequestMessage, you have a couple of options:

By referencing System.Net.Http.Formatting, you can use the ReadAsFormDataAsync() extension method.

var formData = await message.Content.ReadAsFormDataAsync();

If you don't want to include that library, you can do the same thing manually by using the HttpUtility helper class in System.Web.

public async Task<NameValueCollection> ParseFormDataAsync(HttpRequestMessage message)
{
    string formString = await message.Content.ReadAsStringAsync();
    var formData = HttpUtility.ParseQueryString(formString);
    return formData;
}

Upvotes: 2

Kevin
Kevin

Reputation: 1200

Try this:

HttpContext.Current.Request.Form["key"];

Upvotes: -1

Philip
Philip

Reputation: 3424

As an alternative to Aliostad's method, one can do:

public void Post(HttpRequestMessage request)
{
    NameValueCollection formData = await request.Content.ReadAsFormDataAsync();
}

Upvotes: 5

tugberk
tugberk

Reputation: 58484

Well, it is not possible because HttpRequestMessage doesn't provide that kind of collection out of the box.

However, if your application is hosted under ASP.NET, you can get to the current HttpContext object and get the form values from there:

public class CarsController : ApiController {

    public string[] Get() {

        var httpContext = (HttpContextWrapper)Request.Properties["MS_HttpContext"];
        var foo = httpContext.Request.Form["Foo"];

        return new string[] { 
            "Car 1",
            "Car 2",
            "Car 3"
        };
    }
}

But I am not sure if this is the best way of doing this kind of stuff.

Upvotes: 19

Aliostad
Aliostad

Reputation: 81680

ASP.NET Web API has become significantly more robust in dealing with different HTTP scenarios - especially streaming. As such only media type formatters normally touch the content and have to make a coherence of the content.

In ASP.NET MVC, application/x-www-form-urlencoded content type is a first class citizen (and treated especially since this is the content type of 95% of the POST requests) and we have the FormsCollection to provide dictionary access in access, whenever it is defined as an input parameter.

In ASP.NET Web API, application/x-www-form-urlencoded is yet another content type, and supposed to be read by its MediaTypeFormatter. As such ASP.NET Web API cannot make any assumption about the Forms.

The normal approach in ASP.NET Web API is to represent the form as a model so the media type formatter deserializes it. Alternative is to define the actions's parameter as NameValueCollection:

public void Post(NameValueCollection formData)
{
   var value = formData["key"];
}

Upvotes: 36

Related Questions