i have this webapi controller:
using Microsoft.AspNetCore.Mvc;
namespace Crud.Controllers
{
[ApiController]
[Produces("application/json")]
[Route("api/[controller]")]
[Route("api/store")]
public class PostsController : Controller
{
[HttpGet("MyName", Name = "name")]
public string GetName()
{
return "just my full name";
}
}
}
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>b0()
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>b5_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.<>cDisplayClass4_0.<Build>b0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>cDisplayClass15_0.<UseStartup>b1(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>cDisplayClass0_0.<Configure>gMiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>cDisplayClass2_0.<Configure>b0(IApplicationBuilder app)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>cDisplayClass0_0.<Configure>b0(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.