的WebAPI的帮助和多发性路线 [英] WebApi help and multipe routes
问题描述
我甲肝一个Web API项目(C#,asp.net MVC),我需要能够既叫行为,使用模板API / {控制器} / {行动} / {ID}和API / {}控制器/ {ID}。
要做到这一点,我添加了2路线API控制器:
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 });
所以,现在如果我有控制器和myController的方法删除,我可以把它删除这两个API / myController的/删除和删除API / myController的;
另外我有自动生成的API帮助,我已经添加了路由的第二个变种之后 - 一些方法是在帮助现在显示两次
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.
我要的是有只有一个帮助参考,每个动作。
是否可以?
或者,也许什么不对我的路由和我可以有多个GET / POST方法,只是用API / {}控制器/(编号)的模板?
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?
推荐答案
这里的关键词是自动生成API帮助。你有两个途径,所以你的帮助会列出他们俩。围绕这一点的唯一方法是不使用自动生成的帮助。但是,如果有两条路线是做一些事情,的两个的应记录在案,否则,你应该只需要开始一个路由。
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.
这篇关于的WebAPI的帮助和多发性路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!