该约束输入“列举HTTPMethod'的路由必须有一个字符串值 [英] The Constraint Entry 'httpMethod' on the Route Must Have a String Value
问题描述
我有一个asp.net的Web API项目,并在我的 WebApiConfig
文件,我已经定义了以下路线:
config.Routes.MapHttpRoute(
名称:网络API获取
routeTemplate:API / {}控制器,
默认:新{行动=GET},
限制:新{列举HTTPMethod =新HttpMethodConstraint(GET)}
);
有关集成测试的目的,我想打一个请求 HttpSelfHostServer
来验证我们接收到正确的数据API调用回来。我使的Htt prequestMessage
如下:
VAR列举HTTPMethod =新列举HTTPMethod(GET);
VAR要求=新的Htt prequestMessage(列举HTTPMethod,HTTP://本地主机:XXXX / API /用户/);
VAR的结果= _client.SendAsync(要求)。结果;
我希望这会呼吁UserController中Get方法,然后返回结果在该方法中定义。但是,我反而得到以下异常:
System.InvalidOperationException:与路径模板API / {}控制器的路线上的约束输入列举HTTPMethod'必须有一个字符串值,或者是它实现IHttpRouteConstraint
这同一个URL(的http://本地主机:XXXX / API /用户/
)的作品没有错误,当我在浏览器中使用它,所以我pretty确保该问题已成为我送过的HttpClient
请求到 HttpSelfHostServer
的方式。我已经使用 HttpMethod.Get
不变,而不是尝试过,但也抛出了同样的错误。
没有人有任何想法我怎么能解决这个问题?
请确保您使用<$c$c>proper键入 您的约束:
的约束:新{列举HTTPMethod =新System.Web.Http.Routing.HttpMethodConstraint(HttpMethod.Get)}
我猜你是使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.routing.httpmethodconstraint.aspx\"><$c$c>System.Web.Routing.HttpMethodConstraint$c$c>这是用于ASP.NET MVC路由一个完全不同的类,并且具有无关的ASP.NET Web API的路由。
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?
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.
这篇关于该约束输入“列举HTTPMethod'的路由必须有一个字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!