Why do WebApi routing config defaults not include the action param? - c#

I am a bit new to WebApi so maybe someone can explain this to me, the default route added for WebApis is:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
So given an ApiController that looks like this:
public class LookupController : ApiController
{
[HttpGet]
public IHttpActionResult GetCountries()
{
// do stuff
return Ok();
}
[HttpGet]
public IHttpActionResult GetStates()
{
// do stuff
return Ok();
}
}
How would it know which action to call? It wouldn't right?
Shouldn't the default Route be more like:
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Or should you only have one GET/UPDATE/DELETE etc per ApiController? Which really wouldn't cater for some scenarios...

Web API figures out which method to call based on routing data. You don't need to specify the action because Web API will use the verb(GET, POST,DELETE,etc) of the request.
If you would like to have multiple GET actions in a controller, you can specify a route for each action.
[Route("api/lookup/countries"]
[HttpGet]
public IHttpActionResult GetCountries()
{
// do stuff
return Ok();
}
[Route("api/lookup/states"]
[HttpGet]
public IHttpActionResult GetStates()
{
// do stuff
return Ok();
}
More information is available here http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

Strictly speaking your controller should never look like that. Out of the box the idea is that a controller handles requests for a single entity (e.g. Customer) and the operations, which map to HTTP verbs, operate against that entity. So under normal circumstances your controller would look something like the following (created using scaffolding against a simple model class, method bodies omitted for brevity):
public class CustomerController : ApiController
{
public IQueryable<Customer> GetCustomers()
{
}
[ResponseType(typeof(Customer))]
public IHttpActionResult GetCustomer(int id)
{
}
[ResponseType(typeof(void))]
public IHttpActionResult PutCustomer(int id, Customer customer)
{
}
[ResponseType(typeof(Customer))]
public IHttpActionResult PostCustomer(Customer customer)
{
}
[ResponseType(typeof(Customer))]
public IHttpActionResult DeleteCustomer(int id)
{
}
}
If you have more business-oriented operations to perform (e.g. BillCustomer), rather than just the basic CRUD operations, I would suggest creating a separate route for those. We did that in one of our applications and it created a nice logical separation. For example:
config.Routes.MapHttpRoute(
name: "RestApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "RpcApi",
routeTemplate: "rpc/{controller}/{action}"
);

Related

How can I add multiple routes in Web.Api

I have created a asp.net web.api. I have one controller named Books which has 2 methods that look like this..
public IHttpActionResult Read(string pass, string Id)
public IHttpActionResult Update(string pass, string Id)
How do I add routes for both methods in my webApiConfig file?
Right now I only have this that work on Read method.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{pass}/{Id}",
defaults: new { transationId = RouteParameter.Optional }
);
This one I can access like this..
Api/Books/xxxxpasscodexxx/1
How would the route for the update method look like?
Change your routing to
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{pass}/{Id}",
defaults: new { transationId = RouteParameter.Optional }
);
you can use the current default route for update also.web api will provide [FromUri] attribute for reading action parameters from URI of Httprequestmessage .
[HttpPut]
public IHttpActionResult Update([FromUri]string pass, [FromUri]string Id)
{
//do your stuff here and return
}
your request should be Api/Books?password=xxxx&string=abc
Add Attribute
[HttpGet]
public IHttpActionResult Read(string pass,int id)
[HttpGet]
public IHttpActionResult Update(string pass,int id)
Add the routing in webapiconfig
config.Routes.MapHttpRoute(
name: "DefaultActionApi",
routeTemplate: "api/{controller}/{action}/{pass}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Note: this must be above the existing route, otherwise it will take precedence.
Please take a look here. You can mark your Controller as APIController, specify the general route and then define the http verb and method path with the Route parameter. This will overwrite your Routes.MapHttpRoute definition.

Why do my WebAPI RESTful routes throw 404 errors?

On a side-project I am working on, I am creating a RESTful API using WebAPI 2.2. The thing I'm working on is a means of accessing settings for a game. An example of the kind of routes I am trying to accomplish are as follows:
http://x/api/GameSettings/ <-- Returns all settings
http://x/api/GameSettings/audio <-- Returns the 'audio' category
http://x/api/GameSettings/audio/volume <-- Returns the key 'volume' in category audio
Note: the examples are all Get requests.
I've implemented the following controller...
public class GameSettingsController : ApiController
{
// GET /api/GameSettings
public HttpResponseMessage Get()
{
// Magic
return Request.CreateResponse(HttpStatusCode.OK, model);
}
public HttpResponseMessage Get(string category)
{
// Similar.
}
public HttpResponseMessage Get(string category, string key)
{
// Slightly different, but still similar.
}
}
I bound up the following MVC routes:
// Only necessary for the main view...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
And, I bound up the following WebAPI routes:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiGeneralCommand",
routeTemplate: "api/{controller}",
defaults: new { controller = "GameSettings" }
);
config.Routes.MapHttpRoute(
name: "ApiCategoryCommands",
routeTemplate: "api/{controller}/{category})",
defaults: new { controller = "GameSettings" }
);
config.Routes.MapHttpRoute(
name: "ApiKeyCommands",
routeTemplate: "api/{controller}/{category}/{key}",
defaults: new { controller = "GameSettings", category = "master" },
constraints: new { key = "[a-z0-9.-]" }
);
...And finally, my Global.asax configuration is set up like so:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
...But one small problem.
When I navigate to http://x/api/GameSettings/audio, I get a 404 error. It's as if the category argument in the request isn't being properly associated to the Get(string category) method on my controller. This leads me to believe my routes are wrong or I'm missing something.
As a sanity check, I tested the route using a non-RESTful syntax, http://x/api/GameSettings?category=audio, which hit a breakpoint and yielded a result. This only reaffirms my theory that the WebAPI routing is off.
As an additional sanity check, I tested http://x/api/GameSettings/ and not only hit a breakpoint set in that function, but returned the expected result.
Question: What is my routing missing, that will allow http://x/api/GameSettings/audio to work like http://x/api/GameSettings?category=audio? I haven't worked with a RESTful API in a while, so I'm sure I'm missing something really dumb.
I would try to use the attribute routing. I believe that should work well for your scenario.
[RoutePrefix("api/GameSettings")]
public class GameSettingsController
{
// GET /api/GameSettings
[HttpGet]
public HttpResponseMessage Get()
{
// Magic
return Request.CreateResponse(HttpStatusCode.OK, model);
}
[Route("{category}")]
[HttpGet]
public HttpResponseMessage Get(string category)
{
// Similar.
}
[Route("{category}/{key}")]
[HttpGet]
public HttpResponseMessage Get(string category, string key)
{
// Slightly different, but still similar.
}
}
I would remove the stuff that you have added to the config.
Hope this helps.
Change the order and try.Because ASP.NET realizes that you have three routes. It will check the top-most route first and if your data can be placed in that route it will not check any more routes.
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiKeyCommands",
routeTemplate: "api/{controller}/{category}/{key}",
defaults: new { controller = "GameSettings", category = "master" },
constraints: new { key = "[a-z0-9.-]" }
);
config.Routes.MapHttpRoute(
name: "ApiCategoryCommands",
routeTemplate: "api/{controller}/{category})",
defaults: new { controller = "GameSettings" }
);
config.Routes.MapHttpRoute(
name: "ApiGeneralCommand",
routeTemplate: "api/{controller}",
defaults: new { controller = "GameSettings" }
);

Web api action selection based on different routes

I have one api Controller with two different routes:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "downloadApi",
routeTemplate: "api/{controller}/{download}/{id}"
);
I want to have these actions with these routes:
1 - For downloading a file: public GetFile()
GET /api/v1/documents/download/id
2 - For getting file info: public GetFileInfo()
GET /api/v1/documents/id
So in my controller I want to have these actions:
public class DocumentsController : apiController
{
// triggers with GET /api/v1/documents/download/id
public HttpResponseMessage GetFile(){}
// triggers with GET /api/v1/documents/id
public HttpResponseMessage GetFileInfo(){}
}
How can I do this?
Thanks
Here's an example to explain my comment. Rather than defining a route in your WebApiConfig.cs that applies to some routes, but not others (which I don't think you can do, but I've never tried so maybe you can), use the Route method attribute to define the route used at the method level.
public class DocumentsController : apiController
{
// triggers with GET /api/v1/documents/download/id
[HttpGet]
[Route("api/v1/documents/download/{id}")]
public HttpResponseMessage GetFile(int id){}
// triggers with GET /api/v1/documents/id
[HttpGet]
[Route("api/v1/documents/{id}")]
public HttpResponseMessage GetFileInfo(int id){}
}
Untested, but it should work

attribute [Route] is not working without [RoutePrefix] in ASP.NET WebApi

Refer to the official ASP.NET attribute routing documentation, it seems that Route attribute can be used without RoutePrefix.
But, in my webapi controller, below cases are happened.
1. Not working. (error: No matched http route found)
public class GroupController : ApiController
{
[Route("api/group/{id}/register")]
public IHttpActionResult Post(int id, InputModel model)
{
return Ok();
}
}
2. Working good.
[RoutePrefix("api/group")]
public class GroupController : ApiController
{
[Route("{id}/register")]
public IHttpActionResult Post(int id, InputModel model)
{
return Ok();
}
}
Is it right that Route attribute should be used with RoutePrefix, or what am I missing?
In addition, below code is my WebApi route config in WebApiConfig.Register class.
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}/{subId}",
defaults: new { id = RouteParameter.Optional, subId = RouteParameter.Optional }
);

API multiple Get methods and routing

I have a controller with only Get Methods
public class DeviceController : ApiController
{
List<Device> machines = new List<Device>();
public IEnumerable<Device> GetAllMachines()
{
//do something
return machines;
}
[HttpGet]
public IEnumerable<Device> GetMachineByID(int id)
{
//do something
return machines;
}
[HttpGet]
public IEnumerable<Device> GetMachinesByKey(string key)
{
//do something
return machines;
}
}
I would like to be able to access these via URL and get the data back
../api/{contorller}/GetAllMachines
../api/{contorller}/GetMachineByID/1001
../api/{contorller}/GetMachiesByKey/HP (machines exist)
When i run the first two in IE developer mode (f12) i get Json back displaying all machines and machine 1001. However when i run GetMachinesByKey/HP i get 404 error.
Also my WebApiConfig looks like this
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{Action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Anyone enlighten me as to what I am doing wrong?
The routing engine is expecting to bind to a variable named id as defined in the route config:
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{Action}/{id}", //<--- here {id} means bind to parameter named 'id'
defaults: new { id = RouteParameter.Optional }
);
In your action GetMachinesByKey(string key) parameter is named key and so the framework isn't connecting those dots for you.
You could pass the parameter in the querystring so using a URL of the form /api/{contorller}/GetMachiesByKey/?key=HP will bind correctly (you may need to change the route config as this doesn't pass an id parameter that the current config will be expecting).
Alternatively I believe you can specify a route for an action using attribute routing. This allows you to decorate your action method with an attribute which tells the framework how the route should be resolved e.g:
[Route("<controller>/GetMachinesByKey/{key}")]
public IEnumerable<Device> GetMachinesByKey(string key)
Use the RoutePrefix and Route attributes.
[RoutePrefix("api/device")]
public class DeviceController : ApiController
{
List<Device> machines = new List<Device>();
[HttpGet]
[Route("Machines")]
public IEnumerable<Device> GetAllMachines()
{
//do something
return machines;
}
[HttpGet]
[Route("Machines/{id:int}")]
public IEnumerable<Device> GetMachineByID(int id)
{
//do something
return machines;
}
[HttpGet]
[Route("Machines/{key}")]
public IEnumerable<Device> GetMachinesByKey(string key)
{
//do something
return machines;
}

Categories

Resources