Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 549
scourge192 Avatar asked Aug 23 '13 13:08

scourge192


People also ask

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .

Which of the following method is required to enable MVC routing for asp net core?

Routing in ASP.NET Core MVC is the mechanism through which incoming requests are mapped to controllers and their actions. This is achieved by adding Routing middleware to the pipeline and using IRouteBuilder to map URL pattern (template) to a controller and action.

How do you handle routing in .NET core?

Routing uses a pair of middleware, registered by UseRouting and UseEndpoints: UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline.

What is Route C#?

Routing is used to map requests to route handlers. Routes are configured when the application starts up, and can extract values from the URL that will be used for request processing.


1 Answers

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.

like image 97
Darin Dimitrov Avatar answered Oct 06 '22 04:10

Darin Dimitrov