我有这两个Web Api控制器,我正在调用控制器方法,GetSingle
来自反应应用程序
public IHttpActionResult GetAllData()
{
var modelitems = new List<TestModel>();
var items = AWEntity.GetProducts();
var i = 0;
foreach (var item in items)
{
var model = new TestModel
{
Id = item.ProductId,
TestName = item.Name,
TestMessage = "My Message" + i,
ReorderPoint = item.ReorderPoint
};
modelitems.Add(model);
i++;
}
return Ok(modelitems);
}
public IHttpActionResult GetSingle()
{
var item = AWEntity.GetProduct();
var model = new TestModel
{
Id = item.ProductId,
TestName = item.Name,
TestMessage = "My Message" + 1,
ReorderPoint = item.ReorderPoint
};
return Ok(model);
}
反应fetch
看起来像
fetch('http://localhost:51620/api/TestApi/GetSingle')
.then(result => result.json())
.then(result => {
this.setState({
items : result
}
)
})
我的WebApi路由配置如下:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
所以,我的问题是每当我调用GetSingle
方法时,我都会收到此错误
在控制台
{
"message": "An error has occurred.",
"exceptionMessage": "Multiple actions were found that match the request: \r\nGetData on type WebAPIComponent.Controllers.TestApiController\r\nGetSingle on type WebAPIComponent.Controllers.TestApiController",
"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.<SendAsync>d__1.MoveNext()"
}
任何想法可能是什么,我尝试添加另一个路由到默认,其中包括动作参数,但似乎没有工作,如果可能,我想避免必须使用自定义路由属性
非常感谢
答案 0 :(得分:0)
啊我想出了答案,路线配置设置错误的方式,带动作的配置应该首先作为一个骚动