ASP.net WebApi Multiple controller types were found that match the URL

梦想的初衷 提交于 2019-12-23 01:25:26

问题


I have added a new HttpPost route "api/export/error/" to my WebApi, using Attribute Routing, and I am getting an error:

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.\r\n\r\nThe request has found the following matching controller types: \r\nMyApp.Controllers.ExportErrorController\r\nMyApp.Controllers.ExportGeneratorController

But I do not have the same routes in different controllers that i see; here are the only routes in those two controllers:

ExportErrorController.cs

[HttpGet] [Route( "api/export/error/total/" )]

[HttpGet] [Route( "api/export/error/channel/{channelId}/" )]

[HttpPost] [Route( "api/export/error/" )]

[HttpDelete] [Route( "api/export/error/{id}/" )]

[HttpPut] [Route( "api/export/error/clear/" )]

ExportGeneratorController.cs

[HttpGet] [Route( "api/2/export/{channelId}/" )]

[HttpPost] [Route( "api/2/export/generate-word/{debugInfo}/" )]

[HttpPost] [Route( "api/2/export/generate-excel/{debugInfo}/" )]

I cannot see any places that have the same between the two controllers


回答1:


Apparently ASP.Net does not evaluate parameter 'types' OR methods when determining the route to use so it found and matched the route text "error" as a potential parameter for 'channelid' even though they were different methods.

Adding a type to the parameter helped it resolve it properly so: [Route( "api/2/export/{channelId}/" )]

Is fixed by changing it to: [Route( "api/2/export/{channelId:int}/" )]



来源:https://stackoverflow.com/questions/36621459/asp-net-webapi-multiple-controller-types-were-found-that-match-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!