Pit*_*k76 3 swagger asp.net-web-api2
我的Web Api 2控制器上有2个Get方法:
// GET: api/ClientApi
public HttpResponseMessage GetAll()
{
IEnumerable<Client> clients = _clientRepository.GetAll();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, clients);
return response;
}
// GET: api/ClientApi/userId
public HttpResponseMessage GetAllClientsForUser(string userId)
{
IEnumerable<Client> clients = _clientRepository.GetAll();
var clientUsers = _clientUserRepository.FindAll(cu => cu.UserId == userId).ToList();
var clientsForUser = clients.Where(i => clientUsers.Select(cu => cu.ClientId).ToList().Contains(i.Id)).ToList();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, clientsForUser);
return response;
}
// GET: api/ClientApi/clientId
public HttpResponseMessage GetClientById(int id)
{
var client = _clientRepository.GetById<Client>(id);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, client);
return response;
}
Run Code Online (Sandbox Code Playgroud)
尽管名称不同,但出现错误:
Swagger 2.0不支持:使用路径“ api / Client”和方法“ GET”的多项操作。
有办法解决这个问题吗?我尝试使用OperationFilter,在StackOverflow上的某个地方找到了它,但这不起作用...
Route.config:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
小智 5
希望对您有所帮助,我已通过这种方式解决了我的问题,转到您的swagger配置文件,在顶部添加以下内容:
using System.Linq;
Run Code Online (Sandbox Code Playgroud)
然后在第175行周围,或使用搜索找到ResolveConflictingActions,您应该找到以下代码行:
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
Run Code Online (Sandbox Code Playgroud)
但是将其注释掉,在其中添加该行代码,并希望可以解决您的问题,继续尝试,因为您可能不只是想要第一个。
在代码行上方,您会看到简短的描述。希望这会有所帮助。
归档时间: |
|
查看次数: |
1576 次 |
最近记录: |