WebAPI,muliple路由被称为[关闭](WebAPI, muliple routes being called [closed])

编程入门 行业动态 更新时间:2024-10-27 06:21:58
WebAPI,muliple路由被称为[关闭](WebAPI, muliple routes being called [closed])

我有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/data

This 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".

更多推荐

本文发布于:2023-08-07 23:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被称为   路由   muliple   WebAPI   called

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!