Given a route definition like this:
routes.MapHttpRoute(
name: "Categories",
routeTemplate: "{controller}/Categories/{entity}/{property}",
defaults: new { action = "Categories", entity = RouteParameter.Optional, property = "Name" }
);
And a controller actions declared like this:
public IEnumerable<Category> Categories(string property);
public IEnumerable<Category> Categories(string entity, string property);
The intent is that the controller represent a specific entity, and it can aggregate other entities. The returned Categories contains a groupby and count of property. So if the controller is UserController and the user Entity has a Name property, you could call User/Categories/Name
and get a result showing distinct names and how many have each name.
If however a User also have an Address entity, and it has a ZipCode I could call User/Categories/Address/ZipCode
and expect a result showing how many Users live at what ZipCodes.
The problem here is that the string entity
parameter is of type string
rather than type Type
, so I have to dirty up my action with code to convert this to a Type instance representing the Entity, and throwing if it is an incorrect string.
What I would like is to instead declare my second action like this:
public IEnumerable<Category> Categories(Type entity, string property);
But then I need to deserialize the string with a custom deserializer. I allready have custom deserializers aka MediaTypeFormatters
for things comming from the content body. My problem here is that in this case the source is a URI parameter rather than the content body.
Which leads to my questions:
- Will a MediaTypeFormatter also work for URI parameters?
- If not, what construct must I implement and hook up how to achieve what I want?