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

后端 未结 16 1985
难免孤独
难免孤独 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 Get(string moniker, bool param1 = false)
    
      [MapToApiVersion("1.1")]
      [Route("{moniker}", Name = "GetTest11")] // Change name here for a different version
      public async Task Get(string moniker)
    

    And pass in the version in the URL:

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

提交回复
热议问题