the web application will compile and run OK but an exception will be raised at endpoints.MapControllers(); in startup.cs only when a GET request to "http://localhost:6666/api/posts/myname" is sent...
System.InvalidOperationException: 'The following errors occurred with attribute routing information:
Error 1:
Attribute routes with the same name 'anything' must have the same template:
Action: 'Crud.Controllers.PostsController.GetName (Crud)' - Template: 'api/Posts/MyName'
Action: 'Crud.Controllers.PostsController.GetName (Crud)' - Template: 'api/store/MyName''
exception details
System.InvalidOperationException
HResult=0x80131509
Message=The following errors occurred with attribute routing information:
Error 1:
Attribute routes with the same name 'anything' must have the same template:
Action: 'Crud.Controllers.PostsController.GetName (Crud)' - Template: 'api/Posts/MyName'
Action: 'Crud.Controllers.PostsController.GetName (Crud)' - Template: 'api/store/MyName'
Source=Microsoft.AspNetCore.Mvc.Core
StackTrace:
at Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory.Flatten[TResult](ApplicationModel application, Func
5 flattener) at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorBuilder.Build(ApplicationModel application) at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors() at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection() at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize() at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.GetChangeToken() at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.<Subscribe>b__0() at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func
1 changeTokenProducer, Action changeTokenConsumer)
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe()
at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource..ctor(ControllerActionEndpointDataSourceIdProvider dataSourceIdProvider, IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory, OrderedEndpointsSequenceProvider orderSequence)
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints)
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints)
at Crud.Startup.<>c.<Configure>b__5_0(IEndpointRouteBuilder endpoints) in C:\Users\esam\Downloads\JsCrudOperationsDemo\Crud\Startup.cs:line 59
at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure)
at Crud.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Users\esam\Downloads\JsCrudOperationsDemo\Crud\Startup.cs:line 57
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.<StartAsync>d__31.MoveNext()
basically the exception happens when more than one route attribute is used on the controller and a named route is used.
how to solve the problem because i can't see any logical problem in using 2 routes that point to one action that is named.
To put it differently to what
@Jerry Cai-MSFT
already explained:
The solution would be to just delete the "Name" from the HttpGet declaration:
Instead of:
[HttpGet("MyName", Name = "name")]
[HttpGet("MyName")]
The error message told you the reason
Attribute routes with the same name 'anything' must have the same template
The 'Name' in HttpGet is used to generate the links.Route names must be unique application-wide. check this official site:
routing
But the Name in your code will match both api/[controller]/MyName and api/store/MyName.
So you can delete the Name, or delete one Route on controller.
Best Regards,
Jerry Cai
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
I just like to keep the routing simple and the naming conventions of the CRUD methods simple as well. You can review the WebAPI controllers in the GitHub example solution.
https://github.com/darnold924/PublishingCompany