1

I'm trying to integrate Swagger into a somewhat big vanilla ASP.NET MVC app. I was going to use Swashbuckle or NSwag after reading this article. The problem is my controllers are inhereted from the generic base class like this:

public class BookController : BaseController<Book>

And then I have a GET method in a base class:

public abstract class BaseController<T> : ApiController where T : SomeEntityConstraint
{
    ...
    public virtual IHttpActionResult Get(int id)
    {
        T queryResult = _repository.Get(id)
        ...
        return Ok(queryResult);
    }
}

I was planning to add [SwaggerResponse(HttpStatusCode.OK, typeof(T))] attribute to a method and this is where I got CSharp'd hard. Apparetnly one can't simply use generic type parameters in an attribute. They must be known at compile time.

I can see from here that ASP.NET Core has a neat ActionResult<TValue> class. But I can't find a substitute in a vanilla ASP.NET. I wonder what I can do in situation like this without introducing unwanted coupling and methods overriding in concrete Controller classes implementation or moving the project to a brand new ASP.NET Core.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
SimpleV
  • 396
  • 4
  • 14

1 Answers1

0

I’m pretty sure you can implement an own ActionResult class which inherits from IHttpActionResult and just acts as proxy... i think NSwag will pick it up as if it were the ASP.NET Core one...

Disclaimer: I’m one of the devs of NSwag...

Rico Suter
  • 11,548
  • 6
  • 67
  • 93