POST throws HttpRequestMessage does not contain a definition for the form

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

Request.Form["parameterNameHere"]

I am trying to do this, but I am getting an error

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

Method in question

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.

+4
source share
1 answer

You must pass your object to the request body and get the values ​​from the body:

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

Or if you need a line:

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

Source: https://habr.com/ru/post/1537946/


All Articles