C# WebAPI 路由模板中的通配符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19725634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:45:07  来源:igfitidea点击:

Wildcard in WebAPI Route template

c#asp.netasp.net-mvcasp.net-web-api

提问by ValidfroM

I have set up a route template:

我已经设置了一个路由模板:

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}/{*wildcard}",
        defaults: new { id = RouteParameter.Optional }
        );

controller's action signature:

控制器的动作签名:

public IEnumerable<object> Get(Int64 id,string abc)

I tried to match it with URL http://mymachine.com/api/Individuals/1?abc=4, but it gives me an exception

我试图将它与 URL http://mymachine.com/api/Individuals/1?abc=4匹配,但它给了我一个例外

{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":" at System.Web.Http.ValueProviders.Providers.RouteDataValueProvider.d__4.MoveNext()\r\n

{"$id":"1","Message":"发生错误。","ExceptionMessage":"未将对象引用设置为对象的实例。","ExceptionType":"System.NullReferenceException", "StackTrace":" 在 System.Web.Http.ValueProviders.Providers.RouteDataValueProvider.d__4.MoveNext()\r\n

Strangely, http://mymachine.com/api/Individuals?id=1&abc=4does match with the controller,action and parameters.

奇怪的是,http://mymachine.com/api/Individuals?id=1& abc=4确实与控制器、动作和参数匹配。

I thought "{id}" of "api/{controller}/{id}/{*wildcard}" will work.

我想: “ (编号)的” “API / {控制器} / {ID}/ {*}通配符”将起作用。

Why?

为什么?

采纳答案by gooid

The wildcard will tell the routing engine to match the rest of the URI to a route parameter (for an example, see "Get Books By Publication Date" section in this article).

通配符将告诉路由引擎到URI的其他部分相匹配的路由参数(例如,请参阅“获取书籍出版日期”一节本文中)。

It does not mean match any arbitrary named variable to any parameter - this is something WebApi does by default with items on the querystring regardless of your route configuration (hence why you second URI worked - it didn't match any route).

这并不意味着将任何任意命名变量与任何参数匹配 - 这是 WebApi 默认情况下对查询字符串上的项目所做的事情,而不管您的路由配置(因此您的第二个 URI 工作 - 它不匹配任何路由)。

It did not match {id}in your route because it was expecting a parameter called {wildcard}that was not marked as optional.

{id}在您的路由中不匹配,因为它期望调用{wildcard}一个未标记为可选的参数。

To illustrate, if you simply changed 'wildcard' to 'abc':

为了说明,如果您只是将 'wildcard' 更改为 'abc':

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}/{*abc}",
    defaults: new {id = RouteParameter.Optional});

Then it would successfully match the following URI:

然后它将成功匹配以下 URI:

http://mymachine.com/api/Individual/1/a/b/c

http://mymachine.com/api/Individual/1/a/b/c

With the values id=1, abc=a/b/c

随着价值观 id=1, abc=a/b/c

To fix, simply remove the wildcard from your route, so that it looks like this:

要修复,只需从您的路线中删除通配符,使其看起来像这样:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new {id = RouteParameter.Optional});

回答by Nam Hoang

The answer from gooid is correct. I just want to answer the 2nd part of the question:

gooid的回答是正确的。我只想回答问题的第二部分:

Strangely, http://mymachine.com/api/Individuals?id=1&abc=4does match with the controller,action and parameters.

奇怪的是,http://mymachine.com/api/Individuals?id=1& abc=4确实与控制器、动作和参数匹配。

Since you have id = RouteParameter.Optional, if the route doesn't provide id, it still matchesthe route template. The placeholder {*wildcard} looks for the rest of the route and put it into the Route Dictionary under the key "wildcard". In this case, it is empty string (or null).

因为你有 id = RouteParameter.Optional,如果路由没有提供 id,它仍然匹配路由模板。占位符 {*wildcard} 查找路线的其余部分,并将其放入“通配符”键下的路线词典中。在这种情况下,它是空字符串(或 null)。

Remember that the query parameters also go into the Route Dictionary. Therefore, your Route Dictionary will be: { "id": "1", "abc": "4", "wildcard": ""}

请记住,查询参数也会进入路由字典。因此,您的路由字典将是:{ "id": "1", "abc": "4", "wildcard": ""}

For more info, you can look into http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

有关更多信息,您可以查看http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api