0

At the moment my web app accepts post request fine. But I'm trying to read all the properties that are passed through to the object and then resend that object with a couple of properties changed. I'm having trouble viewing the object properties. Usual JSON objects are sent as a string and I can see all the properties that sent. Is there a way to see all the properties of an encoded JSON object?

I've tried examples from How to Get the HTTP Post data in C#?

public ActionResult Post(object value)
{
    string[] keys = Request.Form.AllKeys;
    for (int i= 0; i < keys.Length; i++) 
    {
       Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
    }

    return new HttpStatusCodeResult(200);
}

But I get a compiler error indicating that HttpRequestMessage does not contain a definition for 'Form'.

I also tried Request.Form["payload"]; or Request["payload"] then i am presented with the error Cannot applying indexing to an expression of type 'HttpRequestMessage'

I would like to know the properties that are sent before I create the class. Do I have to create the class first then cast it to the object?

Master
  • 2,038
  • 2
  • 27
  • 77
  • Based on what you show I would expect your `Post` method is in a Controller class that is instantiated by the asp.net-mvc framework. In that context it doesn't make sense that Request is of type HttpRequestMessage. Please provide an [mcve]. – rene Sep 30 '17 at 16:01
  • Hi @rene, i just created a new asp.net mvc web api project and I get the same results. https://imgur.com/a/ri6Wg – Master Sep 30 '17 at 17:25
  • Why is that controller subclassing ApiController? That means you're not interested in httpforms. – rene Sep 30 '17 at 17:27
  • I'm fairly new to web dev. Those values are set by default when choosing the web api template https://imgur.com/a/e742K Should have I created the project differently? – Master Sep 30 '17 at 17:31
  • But if you want to use html forms that can post you don't use webapi, you should use MVC. That gives you a different controller base class that behaves like you expect. – rene Sep 30 '17 at 17:33

1 Answers1

0
System.Web.HttpContext.Current.Request.Form

this can help you to access Form information in anywhere.

no matter ApiController or anything else

actually for webform and MvcController you can directly access Request.Form, but not in ApiController

Ray H
  • 491
  • 3
  • 7