MVC: passing complex models in GET
MVC: passing complex models in GET

MVC: passing complex models in GET

2014, Jul 08    

I started recently to work on a REST WebAPI project and soon realized I needed a way to pass complex parameters to my GET actions.

For example, in some cases I have “searcher” classes that expose properties like pagination data, text fields, identificators and so on, something like this:

[csharp]
public class Identificator{
public int Id {get;set;}
public string Name {get;set;}
}

public class Searcher{
public int Page {get;set;}
public int PageSize {get;set;}
public Identificator Key {get;set;}
}
[/csharp]

and I use classes like this to filter my data and perform queries.
I could have used the [FromUri] attribute, but seems that it doesn’t correctly deserialize inner classes.

Since it’s perfectly legal to create (on client-side) a JSON string from the “searcher” and pass it on the querystring, I spent an hour or so to create a custom attribute that works like [FromUri], but is able to deserialize the JSON data to the correct model type.

I have created a repo on Git, here’s the link: https://github.com/mizrael/MVC-Json-Model-From-Uri

PS: this is the first post I write using my new MacbookPro 😀

Did you like this post? Then