0
votes

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

2

2 Answers

1
votes

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);
       //...
    }
1
votes

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.