WebApi help and multipe routes

百般思念 提交于 2019-12-11 12:54:25

问题


I hav a web api project (C#, asp.net MVC) where I need to be able to call actions both, using template api/{controller}/{action}/{id} and api/{controller}/{id}. To do this, I've added 2 routes for api controller:

 config.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{action}/{id}",
           defaults: new { id = RouteParameter.Optional },
           constraints: new { action = @"^[a-zA-Z]+$" }
           );

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

So now if I have controller MyController and method Delete, I can call it both DELETE api/MyController/Delete and DELETE api/MyController; Also I have auto-generated api help and after I've added second variant of the routing - some methods are now displayed two times in the help.

What I want is to have only one reference in the help, for each action. Is it possible? Or maybe something is wrong with my routing and I can have multiple GET/POST methods, just using "api/{controller}/{id}" template?


回答1:


The keyword here is "auto-generated" API help. You have two routes, so your help will list them both. The only way around that would be to not use auto-generated help. But, if there's two routes that do something, both should be documented, otherwise, you should only have one route to begin with.



来源:https://stackoverflow.com/questions/19836991/webapi-help-and-multipe-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!