Reputation: 609
I'm trying to get a simple WebAPI project working.
I have a WebApiConfig.cs with this:
config.Routes.MapHttpRoute(
name: "cap-getvehicleoptions",
routeTemplate: "api/{controller}/{action}/{type}/{capid}",
defaults: new { controller="Cap", action="GetVehicleOptions" }
);
Then this controller:
public class CapController : ApiController
{
public string GetVehicleOptions(string type, int capid)
{
var response = CapService.GetVehicleOptions(type, capid);
response.Url = string.Format("/api/cap/getvehicleoptions/{0}/{1}", type, capid.ToString());
return Serialization.Serialize(response);
}
}
When I run try this: http://localhost:62924/api/cap/GetVehicleOptions/car/55555/
I get:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:62924/api/cap/GetVehicleOptions/car/55555/'.","MessageDetail":"No action was found on the controller 'Cap' that matches the request."}
I wondered if it's because of the int capid property, but tried string and that didn't help.
thanks
Upvotes: 0
Views: 39
Reputation: 1196
In your route template, action name is included in the URI. In that case, use attributes to specify the allowed HTTP methods.
Decorate the action with [HttpGet] attribute and it should work.
Upvotes: 1
Reputation: 1134
Check if You configure it properly: Is Your route in WebApiConfig.cs in Register method?
Is Your WebApiConfig added to GlobalConfiguration?
example IN Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
//...
}
Upvotes: 1