In my ASP.Net Core app, I am using NSwag to create my swagger.json file.
When I build my project, I get the following error:
Severity Code Description Project File Line Suppression State
Error MSB3073 The command "dotnet
".nugetpackagesnswag.msbuild13.20.0buildTransitive../tools/Net70/dotnet-nswag.dll"
run aspnetcoretoopenapi.json
/variables:"input=srcApiGateway.ApiApiGateway.Api.csproj,output=srcApiGateway.Api../swagger/ApiGateway.Api.json,configuration=Debug,documentName=Public
API"" exited with code
-1.
ApiGateway.Api srcCat.InfrastructureBuildCreateSwaggerDocument.targets
My controller looks like this:
[ApiController]
[Route("v1/[controller]")]
public class MyController : MyControllerBase
{
[HttpGet("{id}/result")]
public async Task<IActionResult> Get([FromRoute] int id)
{
return await ForwardRequest(HttpMethod.Get,
"http://localhost/v2/result");
}
}
public class MyControllerBase : ControllerBase
{
public async Task<IActionResult> ForwardRequest(HttpMethod httpMethod, string url, object? body = null)
{
// do something.
}
}
When I put the logic from "ForwardRequest" into the MyController and remove the inheritence, everything works fine.
When I start to use the MyControllerBase class, it breaks.
What am I missing?
2
Answers
you need to add
[HttpPost]
or[HttpGet]
or[HttpPut]
etc… attribute to you base class method as well but look the following examples down hereBase class
Implementation
The HttpMethod in the API endpoint parameter can be replaced with a string.
Note: I used [HttpPost] because "object? body" will be fromBody. It can be [HttpGet], but you won’t be able to use it in Swagger.