简体   繁体   中英

SetActualResponseType with same method in the same controller with different routes

SCENARIO:

Web Api 2.2

I don't know if is the best method, but this is already in production as is and I couldn't change it.

I've a controller with same verbs (several GET) but with different routes. For example, controller foo:

 [Route("foo/{Id}")]
    public HttpResponseMessage Get(int Id) //Return objectA

[Route("foo/Search")]
public HttpResponseMessage Get(string criteria) //Return objectB

[Route("foo/Anything")]
public HttpResponseMessage Get(anything bar) //Return objectC

Now I'm creating the Web API Help Documentation and in my HelpPageConfig.cs and I need specify the response type (is different for all methods) with:

config.SetActualResponseType(typeof(objectA), "foo", "Get");

QUESTION:

Is possible to make the same with the other methods? When I tried, it says an error: "the definition is already exists"

One way is use another overload of SetActualResponseType, which accepts parameter names:

config.SetActualResponseType(typeof(ObjectA), "Home", "Get", "Id");
config.SetActualResponseType(typeof(ObjectB), "Home", "Get", "criteria");
config.SetActualResponseType(typeof(ObjectC), "Home", "Get", "bar");

But, since you are using Web Api 2.2, forget about SetActualResponseType and just do:

[Route("foo/{Id}")]
[ResponseType(typeof(ObjectA))]
public HttpResponseMessage Get(int Id) //Return objectA

[Route("foo/Search")]
[ResponseType(typeof(ObjectB))]
public HttpResponseMessage Get(string criteria) //Return objectB

[Route("foo/Anything")]
[ResponseType(typeof(ObjectC))]
public HttpResponseMessage Get(anything bar) //Return objectC

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