Hi there. There are particular cases where you might need to create some sort of custom route for a particular service. Simply assume you have a Category and the Category has a single property of type CategorySubResource and another property of type Category.
How can I do the following?
Code I want to perform.
public class CategoriesService : ApiController{
public IEnumerable<Category> GetAll(){ return ... } // great... This returns the list of them
public Category GetCategory(string id){ return .... } // great.... This returns a single category
public CategorySubResource GetCategorySubResource(string id) { ... } // Runtime exception... ???
public Category GetParentCategory(string id) { ... } // ???
}
Routing code:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
When I had the ability to explicitly put [WebGet] and [WebInvoke] attributes on my methods in version 0.6.0 (WCF WebApi) this was a very simple thing... Now, how can I do this?
public class CategoriesController : ApiController
{
public static IEnumerable<Category> Categories = new List<Category>()
{
new Category(){ Id = "1", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null},
new Category(){ Id = "2", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null},
new Category(){ Id = "3", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null},
new Category(){
Id = "4",
Description = "something here " + Guid.NewGuid().ToString(),
Name = "Some Name " + Guid.NewGuid().ToString(),
ParentId = null,
SomethingElse = new SomethingElse(){ Id = "5", Name = "ASDFASDF"},
Child = new Category{ Id = "7", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null}
}
};
public IEnumerable<Category> GetAll()
{
return Categories;
}
Dot_Net_Guy
None
0 Points
8 Posts
Explicitly declaring UriTemplate
Nov 07, 2012 11:49 PM|LINK
Hi there. There are particular cases where you might need to create some sort of custom route for a particular service. Simply assume you have a Category and the Category has a single property of type CategorySubResource and another property of type Category. How can I do the following?
Code I want to perform.
public class CategoriesService : ApiController{
public IEnumerable<Category> GetAll(){ return ... } // great... This returns the list of them
public Category GetCategory(string id){ return .... } // great.... This returns a single category
public CategorySubResource GetCategorySubResource(string id) { ... } // Runtime exception... ???
public Category GetParentCategory(string id) { ... } // ???
}
Routing code:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
When I had the ability to explicitly put [WebGet] and [WebInvoke] attributes on my methods in version 0.6.0 (WCF WebApi) this was a very simple thing... Now, how can I do this?
Thanks everyone in advance!
Dot_Net_Guy
None
0 Points
8 Posts
Re: Explicitly declaring UriTemplate
Nov 08, 2012 12:34 AM|LINK
Here is the example class I am working with.
public class CategoriesController : ApiController
{
public static IEnumerable<Category> Categories = new List<Category>()
{
new Category(){ Id = "1", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null},
new Category(){ Id = "2", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null},
new Category(){ Id = "3", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null},
new Category(){
Id = "4",
Description = "something here " + Guid.NewGuid().ToString(),
Name = "Some Name " + Guid.NewGuid().ToString(),
ParentId = null,
SomethingElse = new SomethingElse(){ Id = "5", Name = "ASDFASDF"},
Child = new Category{ Id = "7", Description = "something here " + Guid.NewGuid().ToString(), Name = "Some Name " + Guid.NewGuid().ToString(), ParentId = null}
}
};
public IEnumerable<Category> GetAll()
{
return Categories;
}
public Category Get(string id)
{
return Categories.FirstOrDefault(d => d.Id == id);
}
public SomethingElse GetSomethingElse(string id)
{
var item = Categories.FirstOrDefault(d => d.Id == id);
if (item != null)
return item.SomethingElse;
return null;
}
public Category GetChild(string parentId)
{
var item = Categories.FirstOrDefault(d => d.Id == parentId);
if (item != null)
return item.Child;
return null;
}
}
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: Explicitly declaring UriTemplate
Nov 16, 2012 02:46 AM|LINK
you could change your route to the following and explicitly supply the name of the action in your request url:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
Kiran Challa