简体   繁体   中英

web api 2 versioning using attribute routing

I'm having a problem using Attribute Routing where the compiler is showing the error "Type 'ValuesController' already defines a member called 'Get' with the same parameter types".

I have checked that config.MapHttpAttributeRoutes() is in the webapiConfig file

    // GET api/values/5
    [Route("api/values/{id}")]
    public string Get(int id)
    {
        return "value";
    }

    [Route("api/v2/values/{id}")]
    public string Get(int id)
    {
        return "value";
    }

From reading http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 something like this should be possible.

Your code is invalid C#, even aside from Web API. You can't define the same function signature twice. If you rename the second Get to GetNew , or move it to a different controller, it should work.

The problem, as I see it, is that you have two methods with the same name and arguments, which isn't allowed in C# - if you really must have the same method names, you need to overload on the number of arguments, and/or their types:

public string Get(int id, int id2)

Or

public string Get(Guid id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM