locked
Web Api 2 - passing a class as URI parameters RRS feed

  • Question

  • Hi there,

    We've hit a weird problem with passing a complex class in web api 2.  The class looks like this:

    MyClass
    {
    public integer PageNumber {get; set;}
    public integer RowsPerPage {get; set;}
    public dynamic Parameters {get; set;}
    public dynamic OrderBy {get; set;}
    }
    

    This is then used as a parameter in a Controller GET method with a "FromUri" attribute.

    Now the weird thing is, the dynamic Parameters never gets deserialised. The debugger just calls it "Object" and won't open it up. Parameters contains filter parameters for SQL, eg:

    {
    "NameFilter":"A%",
    "MinAgeFilter": 18
    }
    

    I've tried swapping dynamic with a JObject, but I think that's dynamic too; a dictionary also comes out empty.

    If I take each of the Parameters and add them into the controller method, I get a 400 returned.

    Questions:

    a) Is it possible to use dynamics in web api controller parameters? Is there some special way of formatting the Json at the client for dynamic parameters?

    b) How is it possible to mix simple types with objects in controller functions, eg this gets an invalid request returned:

    [Route("mydata/page/{pagenumber}")]
    public Get(int pagenumber, [FromUri] PagingParams request,
                               [FromUri] string NameFilter,[FromUri] int MinAgeFilter)
    
    

    The (unescaped) URI generated from JQuery for that looks something like:

    mydata/page/1?request[PageNumber]=1&request[RowsPerPage]=20&request[OrderBy]=&NameFilter=A%&MinAgeFilter=18

    which throws an invalid request and with the parameters inside PagingParams, something like this:

    mydata/page/1?request[PageNumber]=1&request[RowsPerPage]=20&request[Parameters][NameFilter]=A%&request[Parameters][MinAgeFilter]=18&request[OrderBy]=

    ...which gets to the controller method, but Parameters doesn't contain anything.

    It all seems a bit strange. Obviously I can just split out PagingParams into separate parameters, probably working that into the route, but I'm intrigued to know what the rules are around this.

    Any thoughts?

    Thanks,


    Thanks, R

    • Moved by Caillen Friday, November 22, 2013 9:28 AM Asp.net question.
    Thursday, November 21, 2013 12:55 PM

Answers

  • Hi Reece Williams,

    Please post ASP.NET questions in ASP.NET forums. You could get more efficient responses there.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Just Karl Friday, November 22, 2013 10:13 PM
    • Marked as answer by Just Karl Friday, December 13, 2013 3:57 PM
    Friday, November 22, 2013 9:27 AM