Using WebApi 2.2. In my WebApiConfig.cs I have:
public static void Register(HttpConfiguration configuration)
{
configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
configuration.Routes.MapHttpRoute(
name: "EnvironmentCreateAnnualTargetApi",
routeTemplate: "api/EnvironmentTargets/{WorkLocationGuid}/{UserGuid}/{Year}"
);
configuration.Routes.MapHttpRoute(
name: "EnvironmentUnitConversionApi",
routeTemplate: "api/Conversions/{ConvertFromMeasureId}/{ConvertFromAmount}/{ConvertToMeasureId}/{MeasureDefinition}",
defaults: new
{
ConvertFromMeasureId = RouteParameter.Optional,
ConvertFromAmount = RouteParameter.Optional,
ConvertToMeasureId = RouteParameter.Optional,
MeasureDefinition = RouteParameter.Optional
}
);
}
The route named "EnvironmentCreateAnnualTargetApi" is working but I cannot get the second one to work, having tried many different things and scouring this site.
My Controller is like this:
[RoutePrefix("api/Conversions")]
public class ConversionsController : ApiController
{
Then to the method:
// GET: api/Conversions/7/5000/10/1
[HttpGet, ActionName("Get")]
[AllowAnonymous]
public double Get(int ConvertFromMeasureId, double ConvertFromAmount, int ConvertToMeasureId, int MeasureDefinition)
I've tried it with and without the defaults set in the MapHttpRoute method.
The error I keep getting is:
<Error>
<Message>No HTTP resource was found that matches the
request URI 'http://localhost:50000/api/Conversions/7/10000/10/1'.</Message>
<MessageDetail>No route providing a controller name was
found to match request URI
'http://localhost:50000/api/Conversions/7/10000/10/1'</MessageDetail>
</Error>
I don't understand what I might be getting wrong here. Have tried stuff from many different answers but none the wiser.