gpt4 book ai didi

c# - 如何在 asp.net mvc 中从同一个 api Controller 调用多个 GET 方法?

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

我正在使用 asp.net mvc5 和 webapi 2。这是我的示例 Controller -

public class DataFetchController : ApiController
{
public async Task<HttpResponseMessage> GetDO()
{
return Request.CreateResponse("ok");
}

public async Task<HttpResponseMessage> GetDoes()
{
return Request.CreateResponse("ok");
}
}

在我的 WebApiConfig.cs 文件中,我有以下代码 -

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

当我运行这个项目并尝试浏览“api/DataFetch/getdo”链接时,我遇到了一个错误,显示 -

{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nGetDO on type MagicMeter.Controllers.DataFetchController\r\nGetDoes on type MagicMeter.Controllers.DataFetchController","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

我该如何解决这个问题??

最佳答案

尝试将您的路由配置代码更改为此

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

关于c# - 如何在 asp.net mvc 中从同一个 api Controller 调用多个 GET 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164812/

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