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()
@长沙大鹏 的答案是正确的。
因为 asp.net core 默认建立的 api controller 中的 Get(id) Action的属性都是[HttpGet("{id}", Name = "Get")],后面这个Get就重复了,你可以改成GetXXX之类就行。
正解,感谢
Attribute routes with the same name 'Get' must have the same template:
把你的代码贴上来吧.路由特性错了
代码很简单,写的demo,测试可行性用的
第一个接口控制器:
// POST api/values [HttpPost] public IEnumerable<Bland> Post([FromBody]object Bland) { JObject obj = JObject.Parse(Bland.ToString()); string value = obj["name"].ToString(); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); MySqlConnection con = new MySqlConnection("server=127.0.0.1;database=super;uid=root;pwd=root;charset='utf8';SslMode=None"); var list = con.Query<Bland>(string.Format("SELECT DISTINCT sjxh as 'name' from sjxh where sjpp ='{0}'", value)); return list; }
第二个接口控制器:
// POST: api/Color [HttpPost] public IEnumerable<Bland> Post([FromBody]object Color) { JObject obj = JObject.Parse(Color.ToString()); string bland = obj["bland"].ToString(); string ver= obj["ver"].ToString(); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); MySqlConnection con = new MySqlConnection("server=127.0.0.1;database=super;uid=root;pwd=root;charset='utf8';SslMode=None"); var list = con.Query<Bland>(string.Format("SELECT yanse as 'name' from sjxh where sjpp='{0}' AND sjxh='{1}'", bland,ver)); return list; }
当我新增第三个时就报这个错,我试过删除第一个,保留第二个和第三个,运行是没有问题的,后两个接口访问正常
@陈夏松: 把几个控制器的和action的签名部分发上来.
[HttpPost] public IEnumerable<Bland> Post([FromBody]object Color) 类似这种
这个问题有解决吗?我也碰到了这个问题,就是按直接新增默认的都会报错
因为 asp.net core 默认建立的 api controller 中的 Get(id) Action的属性都是[HttpGet("{id}", Name = "Get")],后面这个Get就重复了,改成GetXXX之类就行。
因为你两个controller里的Get(int id)签名的action的路由名字一样, 都是 [HttpGet("{id}", Name = "Get")],你把其中一个的Name="Get" 这段去掉就好了
– 长沙大鹏 6年前