18 c# swagger swagger-ui asp.net-core asp.net-core-webapi
我HTTP GET
在同一个控制器中有 2 个方法并给我这个错误
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的启动:
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");
});
}
}
Run Code Online (Sandbox Code Playgroud)
小智 28
您可以按如下方式解决:
services.AddSwaggerGen (c =>
{
other configs;
c.ResolveConflictingActions (apiDescriptions => apiDescriptions.First ());
});
//in the Startup.cs class in the ConfigureServices method
Run Code Online (Sandbox Code Playgroud)
或者您可以放置路由来区分您的方法,例如:
[HttpGet("~/getsomething")]
[HttpGet("~/getothersomething")]
Run Code Online (Sandbox Code Playgroud)
小智 14
我将控制器路由更改为以下内容:
[Route("api/[controller]/[action]")]
Run Code Online (Sandbox Code Playgroud)
或者您也可以为操作定义显式路由:
[Route("GetById")]
Run Code Online (Sandbox Code Playgroud)
Der*_*ğlu 12
你需要映射id
到HttpGet
.
[HttpGet("{id}")]
public async Task<DataStoreQuery> GetByIdAsync(int id)
{
try
{
return await _dataStoreService.GetByIdAsync(id);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
当您通过不提供模板来指定 HttpGet 时,Swashbuckle 会尝试为它们使用默认映射。因此发生冲突。
如果方法中有“Name”,那么它也会抛出此错误。要解决这个问题,
更改自
[HttpGet(Name = "GetWeatherForecast")]
[HttpGet(Name = "GetWeatherForecastById")]
Run Code Online (Sandbox Code Playgroud)
到
[HttpGet("GetWeatherForecast")]
[HttpGet("GetWeatherForecastById")]
Run Code Online (Sandbox Code Playgroud)
您还可以将具有相同端点的方法合并为具有可选参数的方法。在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;
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15808 次 |
最近记录: |