0

I'm trying to set up routing for a Get action that has an object as it's only parameter.

I have the following code:

public class LuteImage
{
    public string Id { get; set; }
    public string Dimensions { get; set; }
    public string Quality { get; set; }
}

public class ImageController : ApiController
{
    public HttpResponseMessage Get(LuteImage luteImage)
    {
    }
}

In my WebApiConfig.cs file I have the following route before the default route

config.Routes.MapHttpRoute(
    name: "ImageOptions",
    routeTemplate: "api/image/{id}/{dimensions}/{quality}",
    defaults: new {
        controller = "image",
        dimensions = RouteParameter.Optional,
        quality = RouteParameter.Optional,
    }
);

However, when I navigate to localhost:xxx/api/image/123/300x300/b, the luteImage property is just null.

How do I allow the luteImage object to take the parameters?

Ant
  • 462
  • 1
  • 4
  • 18
  • uhh I highly doubt you can get a complex object across in a GET. You could just accept the 3 parameters and build your object up, though. – Jonesopolis Nov 23 '16 at 14:52
  • @Jonesopolis I thought it would be possible, because as I understand it, doesn't normal MVC routing handle this just fine with `GET` requests? You simply pass the properties and the system handles the conversion to the complex object? – Ant Nov 23 '16 at 14:55
  • 1
    By using `[FromUri]` you can populate a complex object. It's all in the [proposed duplicate](http://stackoverflow.com/a/12916668/4949005). If you don't find your answer there, then please let me know and I'll write an answer for you. – smoksnes Nov 23 '16 at 14:58
  • Thank you @smoksnes. That's exactly what I needed. Do you know the StackOverflow best practice in closing this question? – Ant Nov 23 '16 at 15:04
  • @Ant - I think that you can accept it as a duplicate, if the proposed duplicate actually answers your question. – smoksnes Nov 23 '16 at 15:05
  • 1
    Hey look at that. I did not know that was possible – Jonesopolis Nov 23 '16 at 15:13
  • @smoksnes thank you for your help. – Ant Nov 23 '16 at 15:40
  • 1
    Glad I could help. Good luck with your project. – smoksnes Nov 23 '16 at 15:43

0 Answers0