简体   繁体   English

SetActualResponseType使用相同方法在同一控制器中使用不同的路由

[英]SetActualResponseType with same method in the same controller with different routes

SCENARIO: 场景:

Web Api 2.2 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. 我有一个具有相同动词(多个GET)但具有不同路线的控制器。 For example, controller foo: 例如,控制器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: 现在,我正在创建Web API帮助文档,并在HelpPageConfig.cs ,我需要通过以下方式指定响应类型(对于所有方法而言都是不同的):

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: 一种方法是使用SetActualResponseType的另一重载,该重载接受参数名称:

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: 但是,由于您使用的是Web Api 2.2,因此不必理会SetActualResponseType,而只需执行以下操作:

[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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