Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Damien Green 72 posts 134 karma points
    Aug 21, 2015 @ 14:01
    Damien Green
    0

    Api Controller with multiple parameters

    Hi Guys,

    I'm trying to write an API controller method that takes two parameters. The actual URL would be formatted like this:

    .....ImageGalleryApi/GetImage/1066/1

    In this example, the APIController is called ImageGalleryApiController, GetImage is a method on the controller which takes two ints as parameters, the first being an ID for the Image Gallery, the second being the image number I wish to retrieve.

    The APIController method is simply formatted as:

    public GalleryImage GetImage(int GalleryID, int ImageID).

    If I try to call the method in this way I receive a 404 - not found. I believe this is becuase the default setup of MVC only allows one parameter, an ID, so if I modify the method to:

    public GalleryImage GetImage(int GalleryID)

    It works fine but.... I need to pass in the image number too and I want this to follow rest methodology. In ASP.net MVC, I can attach a UriTemplate to the interface that defines the web method. So I could define UriTemplate = "GetImage/{GalleryID/{ImageID}")] and then have MVC accept multiple parameters.

    Is there a way to achieve the same in Umbraco? If so how do I go about this?

    Many thanks,

    Damien

  • Damien Green 72 posts 134 karma points
    Aug 21, 2015 @ 14:45
    Damien Green
    0

    I figured it out, I needed to add the following route to the routes table:

    RouteTable.Routes.MapHttpRoute( "ImageGallery", "ImageGallery/{action}/{id}/{imageID}", new { controller = "ImageGalleryApi", id = "ID", imageID = "ImageID" } );

    This will now allow me to call methods with two parameters, the URL that will work in this case is:

    http://localhost:5010/ImageGallery/GetFullResolutionImage/1060/1 if I don't specify a specific action (or method) then it will just map everything to the template I've specified.

  • Anders Bjerner 487 posts 2989 karma points MVP 8x admin c-trib
    Aug 21, 2015 @ 15:33
    Anders Bjerner
    0

    Without any further configuration, you should be able to call your controller something like this - that is by specifying any parameters in the query string:

    .....ImageGalleryApi/GetImage?GalleryID=1066&ImageID=1
    

    If the structure of the URLs doesn't really matter, this may be easier to use ;)

  • Damien Green 72 posts 134 karma points
    Aug 21, 2015 @ 16:19
    Damien Green
    0

    Thanks Anders,

    That's the technique I used to use, but we're trying to lose query strings and move to more rest friendly conventions.

    Thanks for the feedback though.

    Cheers,

    Damien

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Aug 23, 2015 @ 08:25
    Robert Foster
    1

    You could also create a Model in Code and using Serialization you can map your data from jquery directly to the model:

    For example:

    Model:

    [DataContract(Name="image")
    public class GalleryImage {
        [DataMember(Name="galleryId")]
        public int GalleryId { get; set; }
    
        [DataMember(Name="imageId")]
        public int ImageId { get; set; }
    }
    

    API Controlller method signature:

    public GalleryImage GetImage(GalleryImage image) 
    

    jQuery Ajax - just serialize (JSON) an object containing the two properties in the GalleryImage object and let WebAPI do it's magic. It'll handle POST, GET, etc.

    As Anders mentioned; no extra configuration required (just make sure you are using the correct path to the WebAPI Controller (which you probably want to make an UmbracoApiController if you haven't already).

    Please forgive any code errors - writing this on the fly from memory :)

Please Sign in or register to post replies

Write your reply to:

Draft