asp.net core 新增到第三个控制器的时候就报错了,不明白为什么,路由必需有对应的模板什么意

2019-01-02 21:44发布

Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HL5TDTQQH680": An unhandled exception was thrown by the application.
System.InvalidOperationException: The following errors occurred with attribute routing information:

Error 1:
Attribute routes with the same name 'Get' must have the same template:
Action: 'WebApplication2.Controllers.aController.Get (WebApplication2)' - Template: 'api/a/{id}'
Action: 'WebApplication2.Controllers.bController.Get (WebApplication2)' - Template: 'api/b/{id}'
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorBuilder.Build(ApplicationModel application)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.GetCollection()
at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.get_ActionDescriptors()
at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.GetTreeRouter()
at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.RouteAsync(RouteContext context)
at Microsoft.AspNetCore.Routing.RouteCollection.<RouteAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()

3条回答
三岁会撩人
2楼-- · 2019-01-02 21:57

@长沙大鹏 的答案是正确的。
因为 asp.net core 默认建立的 api controller 中的 Get(id) Action的属性都是[HttpGet("{id}", Name = "Get")],后面这个Get就重复了,你可以改成GetXXX之类就行。

查看更多
可以哭但决不认输i
3楼-- · 2019-01-02 22:13

Attribute routes with the same name 'Get' must have the same template:

 

把你的代码贴上来吧.路由特性错了

查看更多
时光不老,我们不散
4楼-- · 2019-01-02 22:13

这个问题有解决吗?我也碰到了这个问题,就是按直接新增默认的都会报错

查看更多
登录 后发表回答