Reputation: 16264
I was looking at https://stackoverflow.com/a/15873977 but it didn't work for me.
If my Post method has a parameter named Message
(an object of my own class), and I do not apply the [FromBody]
attribute to it, is it possible to pass the parameter Message
, json serialized and urlEncoded, on the query string instead of in the Post body?
I tried passing ?Message=%7B+%22Sender%22%3A...+%7D
(which if decoded would be Message={ "Sender":... }
) but the Message
parameter is still received as null in the method.
Should the query string key be Message, the name of the parameter, or the class name of the parameter or something else?
Upvotes: 0
Views: 1392
Reputation: 151674
If you have a model Foo:
public class Foo
{
public string Bar { get; set; }
public int Baz { get; set; }
}
And you want to bind this from the query string, then you must address the individual properties:
?Bar=qux&Baz=42
And annotate that the model must be bound from the query string:
public void Bar([FromUri]Foo foo)
{
}
If you really want to send JSON into your action method and not a model, simply bind to a string instead of a model. You can then do whatever you want with the JSON string inside your action method.
Upvotes: 2