gpt4 book ai didi

c# - 在 Web Api 中找到与请求匹配的多个操作

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:54 25 4
gpt4 key购买 nike

当我尝试使用 2 个“Get”方法时,我不断收到此错误

Multiple actions were found that match the request: webapi

我一直在查看有关堆栈上此问题的其他类似问题,但我不明白。

我有 2 个不同的名称并使用“HttpGet”属性

[HttpGet]
public HttpResponseMessage Summary(MyVm vm)
{
return null;
}

[HttpGet]
public HttpResponseMessage FullDetails()
{
return null;
}

最佳答案

你的路线图在WebApiConfig.cs中可能是这样的:

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });

但是为了使用相同的 http 方法执行多个操作,您需要通过这样的路由向 webapi 提供更多信息:

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });

请注意,routeTemplate 现在包含一个操作。这里有更多信息:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

更新:

好吧,现在我想我明白你想要的是什么了:

也许你不需要action url参数,应该用另一种方式描述你所追求的内容。既然你说的是这些方法从同一个实体返回数据,那么就让参数为你做描述。

例如你的两个方法可以变成:

public HttpResponseMessage Get()
{
return null;
}

public HttpResponseMessage Get(MyVm vm)
{
return null;
}

您在 MyVm 对象中传递什么样的数据?如果您只能通过 URI 传递变量,我建议您走那条路。否则,您将需要在请求正文中发送对象,而在执行 GET 时这不是您的 HTTP(尽管它有效,只需在 MyVm 前面使用 [FromBody])。

希望这说明您可以在一个 Controller 中拥有多个 GET 方法,而无需使用操作名称甚至 [HttpGet] 属性。

关于c# - 在 Web Api 中找到与请求匹配的多个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31588878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com