The Constraint Entry 'httpMethod' on the Route Must Have a String Value
Asked Answered
N

1

8

I have an asp.net Web API project, and in my WebApiConfig file, I have the following route defined:

config.Routes.MapHttpRoute(
    name: "Web API Get",
    routeTemplate: "api/{controller}",
    defaults: new { action = "Get" },
    constraints: new { httpMethod = new HttpMethodConstraint("GET") }
    );

For integration testing purposes, I want to make a request to an HttpSelfHostServer to verify that we are receiving the proper data back from the api call. I am making the HttpRequestMessage as follows:

var httpMethod = new HttpMethod("GET");
var request = new HttpRequestMessage(httpMethod, "http://localhost:XXXX/api/User/");
var results = _client.SendAsync(request).Result;

I would expect that this would call the Get method on the UserController and then return the results as defined in that method. However, I instead get the following exception:

System.InvalidOperationException: The constraint entry 'httpMethod' on the route with route template 'api/{controller}' must have a string value or be of a type which implements 'IHttpRouteConstraint'

This same url (http://localhost:XXXX/api/User/) works without error when I use it in the browser, so I am pretty sure the issue has to be in the way I am sending the request to the HttpSelfHostServer through the HttpClient. I have tried using the HttpMethod.Get constant instead, but that also threw the same error.

Does anyone have any idea how I could resolve this issue?

Nuli answered 23/8, 2013 at 13:34 Comment(4)
this might help you emilsblog.lerch.org/2012/05/constraint-for-route-parameter.htmlSaddlebag
I don't have access to the WebApi Url object to use the solution he is proposing. I see that the route direction is likely RouteDirection.UrlGeneration and httpMethod is not being found (hence the exception). How can I get the httpMethod with a string value of "GET" into my request without using the Url.Action() method?Nuli
Okay, I came across the UrlHelper class which seems to do the same thing as the Url object in the controllers. However, now I am getting a new error associated with the configuration object. I'll update my question shortly with more details...Nuli
Oops, configuration object was null at that point. After fixing that I'm back to where I started with the first exception.Nuli
N
20

Make sure that you are using the proper type for your constraint:

constraints: new { httpMethod = new System.Web.Http.Routing.HttpMethodConstraint(HttpMethod.Get) }

I guess you were using System.Web.Routing.HttpMethodConstraint which is an entirely different class used for ASP.NET MVC routing and which has nothing to do with ASP.NET Web API routing.

Neomaneomah answered 23/8, 2013 at 15:7 Comment(4)
Wow, yeah that was it. Thank you, I've been beating my head against that one for a while now.Nuli
Yes, Microsoft created one hell of a mess when they duped every single class for this Web API that was available in MVC but under different assemblies and namespaces. It's a very common cause for mistakes.Neomaneomah
Great answer, that was really well spotted @DarinDimitrov, thanks.Gardy
I ended up here because I was looking for the opposite, incase this is useful to anyone else: new System.Web.Routing.HttpMethodConstraint(HttpMethod.Delete.ToString())Kowalczyk

© 2022 - 2024 — McMap. All rights reserved.