A route named “x” is already in the route collection. Route names must be unique. Exception with ASP.NET MVC 3

后端 未结 16 1908
难免孤独
难免孤独 2020-12-02 16:24

I\'m doing an ASP.NET MVC 3 web service and I keep getting this exception intermittently.

Stack trace:

Server Error in \'/\' Application.

A route n         


        
相关标签:
16条回答
  • 2020-12-02 16:50

    try this code, only change name

    routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapHttpRoute(
              name: "API",
              routeTemplate: "api/{controller}/{action}",
              defaults: new { action = "GetAgentId" }
          );
    
    0 讨论(0)
  • 2020-12-02 16:51

    If you are versioning, and you use two APIs with the same name, you will get this error. If you need the same Get, try changing the Name attribute of the route:

    TestsController.cs:

          [MapToApiVersion("1.0")]
          [Route("{moniker}", Name = "GetTest")]
          public async Task<IHttpActionResult> Get(string moniker, bool param1 = false)
    
      [MapToApiVersion("1.1")]
      [Route("{moniker}", Name = "GetTest11")] // Change name here for a different version
      public async Task<IHttpActionResult> Get(string moniker)
    

    And pass in the version in the URL:

    http://localhost:6600/api/tests/2020?api-version=1.1

    0 讨论(0)
  • 2020-12-02 16:54

    I found out that Global.asax was referring to an old version of the site's DLL file before I renamed it. The DLL was not being cleaned up when I did Build > Clean up because the VS project/solution didn't refer to it any more. It seems that sometimes only the newer version of the DLL was being used, allowing the site to work correctly, but eventually both of them would be loaded causing the route conflicts.

    0 讨论(0)
  • 2020-12-02 16:55

    To fix this problem I had to go into the bin folder on my project, delete all DLL files and then rebuild and this fixed the problem.

    0 讨论(0)
  • 2020-12-02 16:58

    I was manually calling AttributeRoutingHttpConfig.Start() in my Global.asax. Did not notice this auto-generated line at the top of the file which automatically calls it.

    [assembly: WebActivator.PreApplicationStartMethod(typeof(Mev.Events.Web.AttributeRoutingHttpConfig), "Start")]
    
    0 讨论(0)
  • 2020-12-02 16:58

    When publishing to an Azure App Service I had to check the Publish Dialog's "Settings"->"File Publish Options"->"Remove additional files at destination" to get the old project DLL and symbol files removed. Then the site would load.

    This is essentially the current answers (Fleas's) solution at the core. Delete the offending DLL.

    What caused this old DLL to be retained was I was loading an older version of the website (MVC 3~5 templates but different web project with colliding namespaces, since the newer version was a copy of this project made some point in the recent past.) The newer project's DLLs just needed to be deleted. There are various ways to achieve this. I found using a dialog to be the easiest atm. Logging into the file system and axing the files by hand certainly works too.

    0 讨论(0)
提交回复
热议问题