5

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.

Community
  • 1
  • 1
Mike Rouse
  • 1,278
  • 18
  • 34
  • Assuming Utilities.MeasureDefinitions is an enum, have you tried changing the parameter type to int and then manually converting to enum in the body? – Aman B Nov 17 '14 at 13:02
  • Also can you try making the method parameter names equivalent to router parameter names? – Aman B Nov 17 '14 at 13:06
  • Implemented both suggestions and updated my question above accordingly. However, still having the same error being returned. I'm still fairly new to this - is there somewhere I can attach a debugger to so I can try to follow what's happening and perhaps see where it's falling over, do you know? – Mike Rouse Nov 17 '14 at 13:19
  • This seems like a similar issue http://stackoverflow.com/questions/19914237/web-api-2-routing-attributes-work-in-one-controller-but-not-another – Aman B Nov 17 '14 at 14:11
  • Can you comment out your other routes as suggested in the link and see if it works? – Aman B Nov 17 '14 at 14:12
  • If the above comment is valid this might be your answer http://stackoverflow.com/questions/21076172/webapi-routing-map-url-to-api – Aman B Nov 17 '14 at 14:16
  • Thanks. Have stripped it right back, but still get the same message. Am going to try removing the URL parameters and see if I can just get it to hit the Action at all. – Mike Rouse Nov 17 '14 at 15:31

1 Answers1

5

After stripping everything right back and trying random things I eventually found the solution (for me anyway).

Change:

configuration.Routes.MapHttpRoute(
       name: "EnvironmentUnitConversionApi",
       routeTemplate: "api/Conversions/{ConvertFromMeasureId}/{ConvertFromAmount}/{ConvertToMeasureId}/{MeasureDefinition}",
       defaults: [blah]

to:

configuration.Routes.MapHttpRoute(
    name: "EnvironmentConversions",
    routeTemplate: "api/{Controller}/{ConvertFromMeasureId}/{ConvertFromAmount}/{ConvertToMeasureId}/{MeasureDefinition}",
    defaults: new
    {
       Controller = "Conversions",
       ConvertFromMeasureId = RouteParameter.Optional,
       ConvertFromAmount = RouteParameter.Optional,
       ConvertToMeasureId = RouteParameter.Optional,
       MeasureDefinition = RouteParameter.Optional
    }
    );

Suddenly it works. Something about {controller} in the routeTemplate made it work. I have no idea why however (and wonder what impact this is going to have if I have a future route with a similar style of parameters).

Mike Rouse
  • 1,278
  • 18
  • 34