4

I am trying to get POST data in C# and everything I have read says to use

Request.Form["parameterNameHere"]

I am trying that, but I get an error saying

System.Net.Http.HttpRequestMessage does not contain a definition for Form and no extension method for Form.'

The method in question is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.HttpRequest;

namespace TextServer.Controllers
{
public class TextController : ApiController
{
    // POST api/<controller>
    public HttpResponseMessage Post([FromBody]string value)
    {
        string val = Request.Form["test"];
        HttpResponseMessage response = new HttpResponseMessage();
        response.Content = new StringContent("Your message to me was: " + value);
        return response;
    }

Any help is greatly appreciated.

MaxPower
  • 881
  • 1
  • 8
  • 25
  • 3
    `Request.Form` works when `Request` is `System.Web.HttpRequest`. In this case your `Request` object is something else so it doesn't work. What is the class and what does it inherit from? – Chris Apr 24 '14 at 18:37
  • @rene I have changed the above to show the entire class. – MaxPower Apr 24 '14 at 18:54
  • 1
    It seems like you are posting only one string (value). If you want to post a complete object follow this link and get the "test" value as a property from the posted object. This might help you solve your problem: http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1 – Jportelas Apr 24 '14 at 19:15
  • @Jportelas: That looks like a pretty good description of what needs to be done. Might be worth expanding that into a full answer. – Chris Apr 24 '14 at 20:45

1 Answers1

3

You should pass your object in the request body and retrieve values from the body:

public HttpResponseMessage Post([FromBody] SomeModel model)
{
    var value = model.SomeValue;
    ...

Or if all you need is the string:

public HttpResponseMessage Post([FromBody] string value)
{
    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new StringContent("Your message to me was: " + value);
    return response;
}
McCee
  • 1,549
  • 10
  • 19