Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve System.InvalidOperationException using MVC 6 - Same template

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
like image 716
Daniel Jamrozik Avatar asked May 26 '15 13:05

Daniel Jamrozik


1 Answers

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);
    }
like image 191
Pylyp Lebediev Avatar answered Nov 03 '22 04:11

Pylyp Lebediev