首页 新闻 会员 周边 捐助

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

0
悬赏园豆:5 [已解决问题] 解决于 2018-10-28 20:45

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()

陈夏松的主页 陈夏松 | 初学一级 | 园豆:147
提问于:2017-06-27 23:19

因为你两个controller里的Get(int id)签名的action的路由名字一样, 都是 [HttpGet("{id}", Name = "Get")],你把其中一个的Name="Get" 这段去掉就好了

长沙大鹏 6年前
< >
分享
最佳答案
1

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

收获园豆:5
ShareDuck | 菜鸟二级 |园豆:207 | 2018-09-24 15:38

正解,感谢

陈夏松 | 园豆:147 (初学一级) | 2018-10-27 23:37
其他回答(2)
1

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

 

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

吴瑞祥 | 园豆:29449 (高人七级) | 2017-06-27 23:23

 代码很简单,写的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;
        }

当我新增第三个时就报这个错,我试过删除第一个,保留第二个和第三个,运行是没有问题的,后两个接口访问正常

支持(0) 反对(0) 陈夏松 | 园豆:147 (初学一级) | 2017-06-29 09:40

@陈夏松: 把几个控制器的和action的签名部分发上来.

[HttpPost] public IEnumerable<Bland> Post([FromBody]object Color) 类似这种

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2017-06-29 09:46
0

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

Tech.Moonlight | 园豆:215 (菜鸟二级) | 2017-11-27 10:25

因为 asp.net core 默认建立的 api controller 中的 Get(id) Action的属性都是[HttpGet("{id}", Name = "Get")],后面这个Get就重复了,改成GetXXX之类就行。

支持(0) 反对(0) 陈夏松 | 园豆:147 (初学一级) | 2018-10-27 23:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册