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?
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:
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 :)
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
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.
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:
If the structure of the URLs doesn't really matter, this may be easier to use ;)
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
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:
API Controlller method signature:
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 :)
is working on a reply...