JohnC
JohnC

Reputation: 1

Class type object as HttpGet

I am trying to create an action that would look like controller/action?param1=val&param2=val with the HttpGet annotation.

What I have is:

[HttpGet]
public IActionResult Index(SomeClass obj)
{
    // do stuff
    return View(something);
}

I can access the action via controller/Index?obj.param1=val&obj.param2=val, but is there a way to avoid obj.param1 and obj.param2 in the query string and have something like controller/Index?page=val&amount=val.

Putting those parameters in the annotation like this didn't work: [HttpGet("/page={obj.subobject.param1}&amount={obj.subobject.param2}")]

Upvotes: 0

Views: 286

Answers (1)

poke
poke

Reputation: 388023

Assuming the default model binding setup, you can just pass the parameter names directly and ASP.NET Core will automatically put the values into the SomeClass object:

public IActionResult Test(SomeClass obj)
{
    return Json(obj);
}

public class SomeClass
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

When opening the URL /Home/Test?foo=baz&bar=qux you will now see that the object is properly filled with the Foo and Bar properties.

Upvotes: 1

Related Questions