我有2条路由作为我的webAPI的一部分,它们接受相同的参数但是具有不同的路由静态组件。 然而他们都在请求期间被调用。
[HttpGet] [Route("things/{id}/config/{optional?}")] public dynamic ThingsConfig(int id, int optional = 0) { // stuff happens }和
[HttpGet] [Route("things/{id}/data/{optional?}")] public dynamic ThingsData(int id, int optional = 0) { // stuff happens }Global.asax中的路由配置与开箱即用版本相同
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { action = "get", id = RouteParameter.Optional } );如果我注释掉[Route]行,则API会按预期返回404错误。 然而,当访问一条路线时它们存在,那么它们都是例如:
http://localhost/api/things/1/config/将调用ThingsConfig和ThingsData
我在这做错了什么?
更新:
我试图简化问题。
创建一个新的VS 2015解决方案。 文件 - >新建 - >项目
检查是否已选中.NET Framework 4.5在C# - > Web下选择“ASP.NET Web应用程序(.NET Framework)”
选择“web API”
“更改身份验证”到“无身份验证”(稍微简化一下)
对值控制器进行了以下更改:
将RoutePrefix添加到控制器本身 - 这似乎是必需的。 添加了我的两条测试路线 - 所以控制器的头部现在看起来像这样:
[RoutePrefix("api/values")] public class ValuesController : ApiController { [HttpGet] [Route("things/{id}/config/{optional?}")] public dynamic ThingsConfig(int id, int optional = 0) { return "Config"; } [HttpGet] [Route("things/{id}/data/{optional?}")] public dynamic ThingsData(int id, int optional = 0) { return "Data"; }没有对默认框架工作进行其他更改。
然后调用两条路线:
http://localhost:<port>/api/values/things/1/config http://localhost:<port>/api/values/things/1/data这符合我的预期。 您可以单独呼叫每条路线。
但是我的原始解决方案仍设法调用两条路线,任何人都可以建议我可能会改变哪些内容会允许这样做?
I have 2 routes as part of my webAPI, these accept the same parameters but have different static components to the route. Yet they both get called during requests.
[HttpGet] [Route("things/{id}/config/{optional?}")] public dynamic ThingsConfig(int id, int optional = 0) { // stuff happens }And
[HttpGet] [Route("things/{id}/data/{optional?}")] public dynamic ThingsData(int id, int optional = 0) { // stuff happens }My route config in the Global.asax is unchanged from the out of the box version
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { action = "get", id = RouteParameter.Optional } );If i comment out the [Route] lines then the API returns 404 errors as expected. However when they are present when one route is accessed then they both are eg:
http://localhost/api/things/1/config/Will call both ThingsConfig and ThingsData
What have I done wrong here?
UPDATE:
I tried to simplify the problem.
Create a new VS 2015 solution. File -> New -> Project
Check .NET Framework 4.5 is selected Choose "ASP.NET Web Application (.NET Framework)" under C# -> Web
Select "web API"
"Change Authentication" to "No Authentication" (to simplify things a bit)
Made the following changes to the values controller:
Added the RoutePrefix to the controller itself - this seemed to be needed. Added my two test routes - so the head of the controller now looks like this:
[RoutePrefix("api/values")] public class ValuesController : ApiController { [HttpGet] [Route("things/{id}/config/{optional?}")] public dynamic ThingsConfig(int id, int optional = 0) { return "Config"; } [HttpGet] [Route("things/{id}/data/{optional?}")] public dynamic ThingsData(int id, int optional = 0) { return "Data"; }No other changes were made to the default frame work.
Then called the two routes:
http://localhost:<port>/api/values/things/1/config http://localhost:<port>/api/values/things/1/dataThis works as I would have expected. You can call each route independently.
But my original solution still manages to call BOTH routes, can anyone suggest where I might have changed something that would allow this?
最满意答案
想想,你已经创建了api控制器并查看控制器路由注册。
因此,您需要按照以下步骤在项目中引用webapi。
但是由于重复,您的实现会出错。
找到了与请求匹配的多个操作:ThingsConfig类型FailTracker.Controllers.ThingsController ThingsData类型FailTracker.Controllers.ThingsController
It turns out that the issue was with the client - it was actually calling both routes. Testing the raw API with Fiddler did not display the issue.
File this one under "problem between keyboard and chair".
更多推荐
发布评论