Sounds like you want AttributeRouting. AttributeRouting is a fantastic open source project that allows you to configure which action methods respond to which requests.
The feature you should check out for your problem in the AttributeRouting home page is RoutePrefixes. You could prefix one controller with [RoutePrefix("Entities/{id}")]
for the first 2 URLs and a second controller with [RoutePrefix("Entities/{action?}")] for the second 2 URLs.
The simpler alternative to your problem is to rename you singular "Entities/id" to "Entity/id", thus you could easily setup your 4 routes like this:
/api/Entity/1 (singular noun name corresponds to an individual entity)
/api/Entity/1/Delete
/api/Entities (plural noun corresponds to the set of entities)
/api/Entities/Delete (delete this group of entities)
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
Marked as answer by MichaelDBang on Nov 30, 2012 01:26 PM
Thank you Darrell for your reply. Sounds like AttributeRouting is what we're looking for... however, we want to use 1 controller maximum for each entity set. Would this be possible?
MichaelDBang
Member
6 Points
20 Posts
WebAPI MapRoute Assistance
Nov 29, 2012 07:47 PM|LINK
Hello Community,
Learning a lot about MapRouting and have spent WAY too much time trying to get what I need done, so I'm hoping someone can help out. :)
I'm looking to do the following:
/api/Entities/1 <- Views the details entity with id of 1 (this is a string, not int)
/api/Entities/1/Action <- Calls a particular action on the entity with the id of 1.
/api/Entities <- Views the entities set.
/api/Entities/Action <- Calls the particular action on the entities set.
The problem I'm encountering is the last one. That is currently being intercepted by the first case, as the id is a string.
Any assistance would be greatly appreciated!
Thank you,
Michael
DarrellNorto...
All-Star
86555 Points
9624 Posts
Moderator
MVP
Re: WebAPI MapRoute Assistance
Nov 30, 2012 09:44 AM|LINK
Sounds like you want AttributeRouting. AttributeRouting is a fantastic open source project that allows you to configure which action methods respond to which requests.
AttributeRouting home and plenty of code samples: http://attributerouting.net/
Quick overview: http://www.strathweb.com/2012/05/attribute-based-routing-in-asp-net-web-api/
The feature you should check out for your problem in the AttributeRouting home page is RoutePrefixes. You could prefix one controller with [RoutePrefix("Entities/{id}")] for the first 2 URLs and a second controller with [RoutePrefix("Entities/{action?}")] for the second 2 URLs.
The simpler alternative to your problem is to rename you singular "Entities/id" to "Entity/id", thus you could easily setup your 4 routes like this:
/api/Entity/1 (singular noun name corresponds to an individual entity)
/api/Entity/1/Delete
/api/Entities (plural noun corresponds to the set of entities)
/api/Entities/Delete (delete this group of entities)
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
MichaelDBang
Member
6 Points
20 Posts
Re: WebAPI MapRoute Assistance
Nov 30, 2012 10:56 AM|LINK
Thank you Darrell for your reply. Sounds like AttributeRouting is what we're looking for... however, we want to use 1 controller maximum for each entity set. Would this be possible?
MichaelDBang
Member
6 Points
20 Posts
Re: WebAPI MapRoute Assistance
Nov 30, 2012 01:32 PM|LINK
I was able to get this working with one controller. Exactly what we needed... Thanks!
MartinJ.
Member
243 Points
80 Posts
Re: WebAPI MapRoute Assistance
Dec 03, 2012 05:18 PM|LINK
Would you mind sharing your solution?
MichaelDBang
Member
6 Points
20 Posts
Re: WebAPI MapRoute Assistance
Dec 04, 2012 09:19 PM|LINK
Hi Martin... I tried to reply to your personal message, but it wouldn't let me.
Here's what I ended up doing for my calls:
public class EntitiesController : ApiController { [GET( "Api/Entities/Summary" )] public Summary GetSummary() { ... } [GET( "Api/Entities" )] public IQueryable<Entity> Get() { ... } [GET( "Api/Entities/{id}" )] public Entity Get( string id ) { ... } [POST( "Api/Entities/{id}/Acknowledge" )] public void Acknowledge( string id, [FromBody]IEnumerable<Entity> items ) { } }Hope that helps. :)