Wildcard in WebAPI Route template

后端 未结 2 1136
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 10:07

I have set up a route template:

    config.Routes.MapHttpRoute(
        name: \"DefaultApi\",
        routeTemplate: \"api/{controller}/{id}/{*wildcard}\",
          


        
2条回答
  •  情深已故
    2021-01-02 10:44

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

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

    Since you have id = RouteParameter.Optional, if the route doesn't provide id, it still matches the 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).

    Remember that the query parameters also go into the Route Dictionary. Therefore, your Route Dictionary will be: { "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

提交回复
热议问题