关键词:
【中文标题】动作需要 Swagger 的唯一方法/路径组合【英文标题】:Actions require unique method/path combination for Swagger 【发布时间】:2019-06-13 11:53:27 【问题描述】:我在同一个控制器中有 2 个HTTP GET
方法并给我这个错误
HTTP 方法“GET”和路径“api/DataStore”被操作重载 - DPK.HostApi.Controllers.DataStoreController.GetByIdAsync (DPK.HostApi)、DPK.HostApi.Controllers.DataStoreController.GetAllAsync (DPK.HostApi)。操作需要 Swagger 2.0 的唯一方法/路径组合。
我的控制器:
[Route("api/[controller]")]
[ApiController]
public class DataStoreController : ApiControllerBase
private readonly IDataStoreService _dataStoreService;
public DataStoreController(IDataStoreService dataStoreService)
_dataStoreService = dataStoreService;
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] DataStoreCommand dataStoreCommand)
try
if (ModelState.IsValid)
await _dataStoreService.PostAsync(dataStoreCommand);
return Ok();
var errorList = ModelState.Values.SelectMany(m => m.Errors).Select(e => e.ErrorMessage).ToList();
return ValidationProblem();
catch (Exception e)
Console.WriteLine(e);
throw;
[HttpPut]
public async Task<IActionResult> PutAsync([FromBody] DataStoreCommand dataStoreCommand)
try
if (ModelState.IsValid)
await _dataStoreService.PutAsync(dataStoreCommand);
return Ok();
var errorList = ModelState.Values.SelectMany(m => m.Errors).Select(e => e.ErrorMessage).ToList();
return ValidationProblem();
catch (Exception e)
Console.WriteLine(e);
throw;
[HttpDelete]
public async Task<IActionResult> DeleteAsync(int id)
try
if (ModelState.IsValid)
var item = await _dataStoreService.GetByIdAsync(id);
await _dataStoreService.DeleteAsync(item);
return Ok();
var errorList = ModelState.Values.SelectMany(m => m.Errors).Select(e => e.ErrorMessage).ToList();
return ValidationProblem();
catch (Exception e)
Console.WriteLine(e);
throw;
[HttpGet]
public async Task<DataStoreQuery> GetByIdAsync(int id)
try
return await _dataStoreService.GetByIdAsync(id);
catch (Exception e)
Console.WriteLine(e);
throw;
[HttpGet]
public async Task<IEnumerable<DataStoreQuery>> GetAllAsync(string instanceName, string dbname, string userName, string userPass, bool isActive, DateTime? startCreatedDate, DateTime? endCreatedDate, DateTime? startModifiedDate, DateTime? endModifiedDate)
object[] parameters = instanceName, dbname, userName, userPass, isActive, startCreatedDate, endCreatedDate, startModifiedDate, endModifiedDate;
var parameterName = "@instanceName , @dbname , @userName , @userPass , @isActive , @startCreatedDate , @endCreatedDate , @startModifiedDate , @endModifiedDate";
try
return await _dataStoreService.ExecWithStoreProcedure(parameterName, parameters);
catch (Exception e)
Console.WriteLine(e);
throw;
我的创业:
public class Startup
public Startup(IConfiguration configuration)
Configuration = configuration;
public IConfiguration Configuration get;
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c =>
c.SwaggerDoc("v1", new Info
Version = "v1",
Title = " ",
Description = " ",
TermsOfService = "None",
Contact = new Contact() Name = " ", Email = " ", Url = " "
);
);
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
);
【问题讨论】:
【参考方案1】:你可以这样解决:
services.AddSwaggerGen (c =>
other configs;
c.ResolveConflictingActions (apiDescriptions => apiDescriptions.First ());
);
//in the Startup.cs class in the ConfigureServices method
或者您可以放置路线来区分您的方法,例如:
[HttpGet("~/getsomething")]
[HttpGet("~/getothersomething")]
【讨论】:
Santos 第一个解决方案意味着只有第一个动作被记录在 Swagger 中。通常,您希望避免未记录的端点...【参考方案2】:我将控制器路由更改为以下:
[Route("api/[controller]/[action]")]
或者您也可以为操作定义明确的路线:
[Route("GetById")]
【讨论】:
由于某种原因,当我使用[Route]
时,我在文档中没有得到api/v1/[controller]/myroute
,我只得到/myroute
,而所有其他HTTP 动词都有完整的api 路径。第一个解决方案是合理的,因为它似乎将方法名称作为终点。【参考方案3】:
您需要将id
映射到HttpGet
。
[HttpGet("id")]
public async Task<DataStoreQuery> GetByIdAsync(int id)
try
return await _dataStoreService.GetByIdAsync(id);
catch (Exception e)
Console.WriteLine(e);
throw;
当您通过不提供模板来指定 HttpGet 时,Swashbuckle 会尝试为它们使用默认映射。因此发生冲突。
【讨论】:
【参考方案4】:您还可以将具有相同端点的方法合并为一个带有可选参数的方法。在 net core 5 项目中测试的实现示例:
services.AddSwaggerGen(c =>
c.ResolveConflictingActions(apiDescriptions =>
var descriptions = apiDescriptions as ApiDescription[] ?? apiDescriptions.ToArray();
var first = descriptions.First(); // build relative to the 1st method
var parameters = descriptions.SelectMany(d => d.ParameterDescriptions).ToList();
first.ParameterDescriptions.Clear();
// add parameters and make them optional
foreach (var parameter in parameters)
if (first.ParameterDescriptions.All(x => x.Name != parameter.Name))
first.ParameterDescriptions.Add(new ApiParameterDescription
ModelMetadata = parameter.ModelMetadata,
Name = parameter.Name,
ParameterDescriptor = parameter.ParameterDescriptor,
Source = parameter.Source,
IsRequired = false,
DefaultValue = null
);
return first;
);
);
【讨论】:
【参考方案5】:如果方法名称相同,则更改请求方法带参数。 我将请求方法更改为以下:
[HttpGet]
public string products()
// add other code
// ex. (return "products()";)
[HttpGet("id")]
public string products(int id)
// add other code
// ex. (return "products(int id)";)
【讨论】:
动作错误的swagger .net核心API模棱两可的HTTP方法
】动作错误的swagger.net核心API模棱两可的HTTP方法【英文标题】:swagger.netcoreAPIambiguousHTTPmethodforActionError【发布时间】:2018-05-2901:31:51【问题描述】:使用.netCore2API实现Swashbuckle/Swagger我现在在访问swagger.json时收到500错误:NotSupporte... 查看详情
springboot自定义swagger2请求url路径的两种方法(代码片段)
文章目录前言方法一:修改应用根路径方法二:引入Swagger2前端代码总结前言首先,把Swagger2的依赖引进来:<!--swagger版本--><swagger.version>2.7.0</swagger.version><!--swagger--><dependency>&l 查看详情
需要递归地生成文件数组的每个唯一组合
】需要递归地生成文件数组的每个唯一组合【英文标题】:Needtogenerateeveryuniquecombinationofanarrayoffilesrecursively【发布时间】:2011-04-1900:25:39【问题描述】:我研究并发现很多类似的请求,但没有什么是我需要的。这是我的问题。我... 查看详情
.netcoreswaggeractionsrequireauniquemethod/pathcombinationforswagger/openapi3.0.useconf(代码片段)
遇到的问题因为新增了一个控制器方法,从而导致在运行Swagger的时候直接报错,异常如下:SwaggerGeneratorException:Conflictingmethod/pathcombination"POSTapi/UserOperationExample"foractions-WebApi.Controllers.UserOperationExampleController.GetUserInfoByPageList(WebApi)... 查看详情
我需要一年中 3 列的唯一组合计数
】我需要一年中3列的唯一组合计数【英文标题】:Ineedcountsofuniquecombinationsof3columnsinayear【发布时间】:2020-04-2008:31:59【问题描述】:我有这样的数据表:deptsub-deptclasssubmission-dateENGCSA12-06-2012ENGCSA19-08-2012ENGEEA02-04-2012ENGCSA12-08-2013EN... 查看详情
python爬虫编程思想(98):使用selenium实现动作链(组合拳控制浏览器)(代码片段)
在前文介绍的交互动作中,交互动作都是针对某个节点执行的,例如,对于某个input节点输入一个字符串,模拟单击某一个按钮等。但还有另外一类交互动作,它们没有特定的执行对象,比如... 查看详情
springboot之整合swagger2
...档变成了前后端开发人员联系的纽带,变得越来越重要,swagger就是一款让你更好的书写API文档的框架,而且swagger可以完全模拟http请求,入参出参和实际情况差别几乎为零。没有API文档工具之前,大家都是手写API文档的(维护起... 查看详情
生成唯一哈希的最安全方法?
...auniquehash?【发布时间】:2018-05-1601:02:07【问题描述】:我需要生成可以在文件名中使用的唯一标识符,并且可以在给定相同输入值的情况下重现。我需要生成数百万个这样的标识符,因为源输入有数百万个组合。为简单起见,我... 查看详情
一个接一个地执行多个动作(代码片段)
我需要一种用一个请求执行多个struts动作的方法。目标是最小化对服务器的请求的需要。所以我需要的是像“MultiAction”这样的东西,它将一个动作列表作为它应该执行的参数,然后返回这个动作的“组合”结果。例如:客户端... 查看详情
UITableView 多个动作
】UITableView多个动作【英文标题】:UITableViewmultipleactions【发布时间】:2018-11-1208:28:54【问题描述】:在UITableView的编辑模式下,我需要三样东西,前两样用UITableView的委托方法很容易搞定:行左侧的删除(红色-)按钮,行右侧的... 查看详情
Swagger:路径参数问题
】Swagger:路径参数问题【英文标题】:Swagger:IssuewithPathparameter【发布时间】:2015-01-2503:45:39【问题描述】:我正在尝试使用以下路径创建一个swagger文件:路径:/v1/customers/id/summary:但是我马上就收到以下错误:API需要路径参数... 查看详情
在 Nest.js 项目中添加 Swagger 基本路径字段
】在Nest.js项目中添加Swagger基本路径字段【英文标题】:AddSwaggerbasepathfieldinNest.jsproject【发布时间】:2020-06-0307:32:20【问题描述】:我正在尝试使用nestjs/swagger从我的后端创建一个swagger文件,但我遇到了与基本路径相关的问题。... 查看详情
swagger使用注意事项-java
使用swagger后,所有controller中的方法都会自动生成接口;但是并不完美,需要配合swagger的注解来进行接口说明的完善(在代码中添加了注释,方便阅读)。可以导入到Yapi中,免去手动创建接口的麻烦。一、类说明@Api(value="描述",tags... 查看详情
唯一正确的修改jupyternotebook默认路径的方法
唯一正确修改JupyterNotebook的默认路径1、按照网上的方法,先修改了快捷方式的起始位置,发现并不能修改默认路径。2、后来发现“目标”中后面有个参数%USERPROFILE%,它代表的意思是用户配置文件,很明显它决定了目录,于是我... 查看详情
Swagger 继承与组合
】Swagger继承与组合【英文标题】:SwaggerInheritanceandComposition【发布时间】:2015-03-0721:11:24【问题描述】:在我的“简化”API中,所有响应都派生自(继承)基础“响应”类。响应类组成由一个充满元数据的标头和包含用户请求的... 查看详情
使用动作约束时在 MVC 6 中使用 Swagger 的多个 Api 版本
】使用动作约束时在MVC6中使用Swagger的多个Api版本【英文标题】:MultipleApiVersionswithSwaggerinMVC6whenusingactionconstraints【发布时间】:2016-04-0623:25:32【问题描述】:希望有人尝试过使用MVC6和Swagger中的版本化API进行类似的操作,以显示... 查看详情
如何在 OpenAPI (Swagger) 中为同一路径定义不同的查询参数?
】如何在OpenAPI(Swagger)中为同一路径定义不同的查询参数?【英文标题】:HowtodefinedifferentqueryparametersforsamepathinOpenAPI(Swagger)?【发布时间】:2017-03-2214:36:36【问题描述】:我正在使用SwaggerCodegen启动REST服务。我需要对不同的参数有... 查看详情
表中列的唯一值组合
...lumninatable【发布时间】:2013-09-2805:05:49【问题描述】:我需要在表的列中找到所有唯一可能的值组合。例如,对于列值1、2、3、4、5。我希望结果是[1,2],[1,3],[1,4],[1,5],[2,1],[2,3]等。将感谢任何构造查询以查找值组合的指针。谢谢... 查看详情