skip to Main Content

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


  1. you need to add [HttpPost] or [HttpGet] or [HttpPut] etc… attribute to you base class method as well but look the following examples down here

    Base class

    public class MyControllerBase : ControllerBase
    {
        public MyControllerBase()
        {
        }
    
        [HttpGet]
        public async Task DoSomething()
        {
           //enter code here...
        }
    }
    

    Implementation

    [ApiController]
    [Route("/persons")]
    public class PersonCheckController : MyControllerBase
    {
        public PersonCheckController() : base()
        {
        }
        
        [HttpGet]
        [Route("{id:guid}")]
        public async Task<Person> GetPersonById([FromRoute] Guid id)
        {
          //get person by id
        }
    }
    
    Login or Signup to reply.
  2. 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.

    using Microsoft.AspNetCore.Mvc;
    
    namespace WebAppTestController.Controllers
    {
      [ApiController]
      [Route("[controller]")]
      public class MyController : MyControllerBase
      {
        [HttpGet("{id}/result")]
        public async Task<IActionResult> Get([FromRoute] int id)
        {
          return await ForwardRequest(HttpMethod.Get.ToString(), "http://localhost/v2/result");
        }
      }
    
      public class MyControllerBase : ControllerBase
      {
        [HttpPost]
        public async Task<IActionResult> ForwardRequest(string method, string url, object? body = null)
        {
          var httpMethod = new HttpMethod(method);
    
          return Ok();
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search