I'm new to ASP.NET in general and have recently come across this error which I can't find on the web (maybe because of the recent release of MVC 6?)
System.InvalidOperationException The following errors occurred with attribute routing information:
Error 1:
Attribute routes with the same name 'GetByIdRoute' must have the same template: Action: 'Appname.Web.Controllers.MemberController.GetById' - Template: 'api/Member/{id:int}' Action: 'Appname.Web.Controllers.PaymentController.GetById' - Template: 'api/Payment/{id:int}' Action: 'Appname.Web.Controllers.PlanController.GetById' - Template: 'api/Plan/{id:int}'
This gives a code snipper to startup.cs
Line 73:
Line 74: // Add MVC to the request pipeline.
Line 75: app.UseMvc(routes =>
Line 76: {
Line 77: routes.MapRoute(
with line 75 highlighted
and this:
at Microsoft.AspNet.Mvc.ControllerActionDescriptorBuilder.Build(ApplicationModel application)
at Microsoft.AspNet.Mvc.Core.ControllerActionDescriptorProvider.GetDescriptors()
at Microsoft.AspNet.Mvc.Core.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
at Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.GetCollection()
at Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.get_ActionDescriptors()
at Microsoft.AspNet.Mvc.Routing.AttributeRoute.GetInnerRoute()
at Microsoft.AspNet.Mvc.Routing.AttributeRoute..ctor(IRouter target, IActionDescriptorsCollectionProvider actionDescriptorsCollectionProvider, IInlineConstraintResolver constraintResolver, ILoggerFactory loggerFactory)
at Microsoft.AspNet.Mvc.Routing.AttributeRouting.CreateAttributeMegaRoute(IRouter target, IServiceProvider services)
at Microsoft.AspNet.Builder.BuilderExtensions.UseMvc(IApplicationBuilder app, Action<IRouteBuilder> configureRoutes)
at Appname.Web.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) in ... Startup.cs:line 75
You can specify route name.
In Web API, every route has a name. Route names are useful for generating links, so that you can include a link in an HTTP response.
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-names
[HttpGet("{id}", Name = "GetPerson")]
public IActionResult Get(int id)
{
var item = this.PeopleRepository.GetById(id);
if (item == null)
{
return this.HttpNotFound();
}
return new ObjectResult(item);
}
By doing that you can have another controller with the same action name (but different route name)
[HttpGet("{id}", Name = "GetPurchase")]
public IActionResult Get(int id)
{
var item = this.PurchaseRepository.GetById(id);
if (item == null)
{
return this.HttpNotFound();
}
return new ObjectResult(item);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With