ApiController GET Action naming - asp.net-mvc

I have an ApiController with multiple GET Actions. The problem is that I wan't to name my actions without "Get" in the start of their names.
For instance, I can have an Action named "GetImage" and it will work just fine.
If I will name it "UpdateImage" it wont call the Action, because it probably want an explicit "Get" in the start of the action name.
I can solve it by defining different route for each action I want to use, but I am sure there must be an easier way achieving it.
I also tried [HttpGet] attribute and unfortunately it didn't do the trick.
My Route Config:
routes.MapHttpRoute(
name: "ImagesApi",
routeTemplate: "api/images/{action}/{id}",
defaults: new { controller = "ImageStorageManager",id = RouteParameter.Optional }
);
and I am accessing it by api/images/GetImage or api/images/UpdateImage

The way I've been creating api controller that aren't just for a single object might help you. I got the approach from John Papa's SPA talk on PluralSight (I highly recommend that for learning single page applications). He also walks through this in one of the modules.
It has 2 parts.
Part 1, setting up the routes to do the 2 normal scenarios and then added a 3rd for what i want:
// ex: api/persons
routes.MapHttpRoute(
name: ControllerOnly,
routeTemplate: "api/{controller}"
);// ex: api/sessionbriefs
// ex: api/persons/1
routes.MapHttpRoute(
name: ControllerAndId,
routeTemplate: "api/{controller}/{id}",
defaults: null, //defaults: new { id = RouteParameter.Optional } //,
constraints: new { id = #"^\d+$" } // id must be all digits
);
// ex: api/lookups/all
// ex: api/lookups/rooms
routes.MapHttpRoute(
name: ControllerAction,
routeTemplate: "api/{controller}/{action}"
);
Part 2, in the lookups controler (in John Papa's case), add an ActionName attribute to the methods:
// GET: api/lookups/rooms
[ActionName("rooms")]
public IEnumerable<Room> GetRooms()
{
return Uow.Rooms.GetAll().OrderBy(r => r.Name);
}
// GET: api/lookups/timeslots
[ActionName("timeslots")]
public IEnumerable<TimeSlot> GetTimeSlots()
{
return Uow.TimeSlots.GetAll().OrderBy(ts => ts.Start);
}

Decorate your action with [HttpGet]. See http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api for details on why, and how ApiController routing works.

Related

Multiple Similar Route not working in ASP.NET MVC 4

I have two routes, but of them are not working on the same time. The one which is on the top working fine but the bottom one not works
config.Routes.MapHttpRoute(
name: "GetKeyWordSearch",
routeTemplate: "api/{controller}/{action}/{keyword}/{Selection}"
//defaults: new { selection = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "GetChapter",
routeTemplate: "api/{controller}/{action}/{bookName}/{chapterNum}/"
//defaults: new {}
);
Any suggestion?
Those 2 routes have the same pattern. There's absolutely no difference between them and the routing engine has no way of disambiguating them. And since routes are evaluated in the order in which they are defined, the first one is always picked.
In order to make this work you need to constrain the parameters using regular expressions or custom constraints. For example:
config.Routes.MapHttpRoute(
name: "GetKeyWordSearch",
routeTemplate: "api/{controller}/{action}/{keyword}/{Selection}",
constraints: new { keyword = #"[0-9]" },
defaults: new { selection = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "GetChapter",
routeTemplate: "api/{controller}/{action}/{bookName}/{chapterNum}"
constraints: new { bookName = #"[a-z]" },
defaults: new { chapterNum = RouteParameter.Optional }
);
In the example I provided, the keyword can only contain numbers whereas the bookName only letters. This way the routing engine will be able to disambiguate the between the following urls and dispatch them to the proper controller action:
api/somecontroller/someaction/123
api/somecontroller/someaction/foo
Obviously you will have to adapt the regular expressions to match your specific needs but remember that there must be some pattern that will make the difference.

Web Api multiple get with same signature routing

I'm building a web api that has multiple get/post calls that have the same signatures. Now I know that in the case of multiple identical calls, you generally have 2 options: separate into different controllers, or use {action} in your routes. I have gone the {action} method as it fits best I believe in most of my controllers. However, in one of my controllers I would prefer not to use the action method.
I have a call like so:
[HttpGet]
public Program Program(string venue, string eventId)
//api/{controller}/{venue}/{eventId}
Now I need a new call
[HttpGet]
public Program ProgramStartTime(string venue, string eventId)
//api/{controller}/{venue}/{eventId}
I know I can add an action name to this and call i.e
api/{controller}/{action}/{venue}/{eventId}
But I feel like it breaks the expected. Is there a way that I could some something like
api/Content/LAA/1/PST
api/Content/LAA/1?PST
Also if I have to go the action route, I currently already have a route I use for other controllers, but it simply uses {id} as its only parameter. Will a new route conflict with this one? Is there a better way to setup my routes?
config.Routes.MapHttpRoute(
name: "...",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional}
);
config.Routes.MapHttpRoute(
name: "...",
routeTemplate: "api/{controller}/{action}/{venue}/{eventId}/{...}/{***}/{###}",
defaults: new {### = RouteParameter.Optional}
);
config.Routes.MapHttpRoute(
name: "...",
routeTemplate: "api/{controller}/{action}/{venue}/{eventId}/{...}",
defaults: new {... = RouteParameter.Optional}
);
config.Routes.MapHttpRoute(
name: "...",
routeTemplate: "api/{controller}/{action}/{venue}",
defaults: new {venue = RouteParameter.Optional}
);
I expect at least one method that would have up to 5 parameters
Here's the answer I found and it does pretty much exactly what I wanted:
config.Routes.MapHttpRoute(
name: "VenuesAllOrStream",
routeTemplate: "api/Racing/{action}",
defaults: new { controller = "Racing", action = "Venues" },
constraints: new { action = "Venues|All|Streaming" }
);
config.Routes.MapHttpRoute(
name: "VenueOrVideo",
routeTemplate: "api/Racing/{venue}/{action}",
defaults: new { controller = "Racing", action = "RaceNumbers" },
constraints: new { action = "RaceNumbers|Video" }
);
config.Routes.MapHttpRoute(
name: "ProgramOrMtp",
routeTemplate: "api/Racing/{venue}/{race}/{action}",
defaults: new { controller = "Racing", action = "Program" },
constraints: new { action = "Program|Mtp", race = #"\d+" }
);
It is important that the VenuesAllOrStream is first as otherwise the VenueOrVideo picks up the route. I most likely will extract out the action constraints into enums later.
Brief note : Setting the action default allows for the route to basically make it an optional parameter. So each route works without the {action} actually being set.

MVC 4 Web API Routing issues

This is my first rodeo with MVC Web API and I'm having some issues understanding the routing aspects. I would like to have a uri template similar to thise:
http://google.com/api/AzureQueue - GET for all items in the queue
http://google.com/api/AzureQueue/DeviceChart/ - GET returns devices and processing time for agent
http://google.com/api/{controller}/{id} <-- default
http://google.com/api/{controller}/{chartType}/{id} where ID is optional
where I'm struggling is:
1. what the french toash do I put in the WebApiConfig.cs file
2. do I need to do anthing special in my controller eg. specifiy NonActions & Actions, Action Names, etc
Any help is appreciated
You are almost there. The default route (in WebApiConfig.cs looks like this:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
There's one very important caveat: the routes are examined in the order that they are declared with the first matching one being used, so the default route needs to go last.
With that out of the way, you need to make a decision, do you want the calls for various chart types to go to one action, or many?
For one action:
WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "AzureQueue",
routeTemplate: "api/AzureQueue/{chartType}/{id}",
defaults: new { controller = "AzureQueue", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
AzureQueueController.cs
public class AzureQueueController : ApiController
{
public string Get(string chartType)
{
return "chart = " + chartType;
}
public string Get(string chartType, int id)
{
return "chart = " + chartType + ",id = " + id.ToString();
}
}
There are two things to notice here. In the anonymous class assigned to defaults, the value for controller decides which controller to route the request to. This can either be in the route template, or simply defined in the class. Also, a request of type Get is automatically sent to an action that starts with Get and has the arguments in the Url that match the template (there are two different cases since id is optional).
This would be my preferred way to go unless the business logic for various charts is different.
On the other hand you could specify this:
WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "AzureQueue",
routeTemplate: "api/AzureQueue/{action}/{id}",
defaults: new { controller = "AzureQueue", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Since I'm using the word action is the template, this will get interpreted as an action name.
AzureQueueController.cs
[HttpGet]
public string DeviceChart()
{
return "chart = DeviceChart" ;
}
[HttpGet]
public string DeviceChart(int id)
{
return "chart = DeviceChart" + ",id = " + id.ToString();
}
Here there is no string argument, that part of the url is being used to decide which action (public method) to use. Also, since the action names don't start with Get, I need to add an attribute [HttpGet] for each method to mark them as being able to receive GET requests.
Good luck with your project.

ASP.NET MVC 4 - API Routing - Specifying Action in the URL

I have a web service written in the ASP.NET MVC 4 framework. Its basic CRUD operations map well to REST verbs, but I have one action that I need to add that does not.
What is the correct way to specify the ability to sometimes pass an ID at the end of the URL that you know will be an Integer type and sometimes have it be an "action" a String type followed by a slash and an int ID? Need help with correct Router map.
Thoughts? Note: I'm more concerned with WebAPI then being 100% REST.
example URLs
// GET list of widgets
http://somedomain.com/api/widget
// GET specific widget
http://somedomain.com/api/widget/1
// POST - take special action on a specific widget - promote
http://somedomain.com/api/widget/promote/1
Would this be the correct way to handle it?
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "DefaultApiExtended",
routeTemplate: "api/{controller}/{action}/{id}"
);
I'd try putting the second pattern (an "action" a String type followed by a slash and an int ID) in at a higher priority than (pass an ID at the end of the URL that you know will be an Integer type).
routes.MapHttpRoute(
name: "ActionWithId",
routeTemplate: "api/widget/{action}/{id}"
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Your action and your ID are not optional as you're always expecting them. If there is no match, the DefaultApi rule should take over processing.
EDIT:
Just thought, if you do want it to be "RESTy" then you could always switch the ID, so for this widget, with this id, perform this action.
routes.MapHttpRoute(
name: "ActionWithId",
routeTemplate: "api/widget/{id}/{action}"
);
Take a look at http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api - specifically section "Routing by Action Name"
Slight change recomended from
http://somedomain.com/api/widget/promote/1
To
http://somedomain.com/api/widgets/1/promote
(Use the plural if possible widget*s*)
Your Controller will need to look like this:
public class WidgetsController : ApiController
{
[HttpPost]
/// POST api/widgets/1/promote
public void Promote(int id)
{
}
// GET api/widgets/1
public string Get(int id)
{
return "value";
}
}
And the routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "DefaultPromoteActionApi",
routeTemplate: "api/{controller}/{id}/{action}",
defaults: new { action = "Promote" }
);
}
Take a look at AttributeRouting; it should be able to accomplish this with ease.

MVC Routing : "Multiple action ids

I am having problems with my MVC Routes.
I am trying to get to the following url ... "http://localhost/api/Countries"
I have defined the following routes in the following order ...
RouteTable.Routes.MapHttpRoute(
name: "Continents",
routeTemplate: "api/countries/Continents",
defaults: new { controller = "countries", Action="Continents" }
);
RouteTable.Routes.MapHttpRoute(
name: "CountryRegions",
routeTemplate: "api/countries/Regions",
defaults: new { controller = "countries", Action = "CountryRegions" }
);
RouteTable.Routes.MapHttpRoute(
name: "CountryByCodeApi",
routeTemplate: "api/{controller}/{countryCode}",
defaults: new { controller="countries", countryCode = System.Web.Http.RouteParameter.Optional }
);
Whenever I go to the desired URL I am getting the error "Multiple actions were found that match the request". This would make sense if the third segment of the routeTemplate property was optional but it was my understanding that by NOT enclosing it in braces that it made it a required segment in the target URL. Obviously "http://localhost/api/countries" does not include "Continents" or "Regions" so why would they be identified as matching the request.
Ya' know. These routes SEEM like a simple enough thing but when you get down to it it's a cryptic as RegEx's !!!
Any thoughts?
The last route definition doesn't provide action name through route definition nor it provides it through route defaults. If route definition should omit it, then add it to defaults as this:
routes.MapRoute(
"CountryByCodeApi",
"api/{controller}/{countryCode}",
new {
controller="countries",
countryCode = RouteParameter.Optional,
action = "CountryCodes"
}
);
Note that this is just the last route definition. The upper couple stays as it is.
public ActionResult CountryCodes(string countryCode)
{
// do whatever you please
}

Resources