Skip to content

define the api version when using generic controllers #412

@ghost

Description

Hi,

i am currently working on a webapi that will work with generic controllers. Plugin can be loaded into the webapi and these will be served by these generic controllers. This works all just fine...
I am using swagger for the documentation of the api

If i want to have versioning on the models/plugins i need to have controllers with the api attribute set accordingly. i can get that part working. I need to change the typeinfo of the controller in order to have swagger generate the documents with the versions.
Is there a way to set the controller version when using a generic controller?

i am using dotnet core 2.1

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace WebApiPluginDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiVersion("0.1")]  // should be dynamic depending on the type version
    public class BaseController<T, K> : Controller where T : class
    {
        private Storage<T> _storage;

        public BaseController(Storage<T> storage)
        {

            _storage = storage;
        }

        [HttpGet()]
        public IEnumerable<T> Get()
        {

            return _storage.GetAll();
        }

        [HttpGet("{id}")]
        public IActionResult Get(K id)
        {
            var result = _storage.GetById(id.ToString());
            if (result != null) { return Ok(result); }
            else
            {
                return NotFound();
            }
        }

        [HttpPost("{id}")]
        [ProducesResponseType(201)]
        [ProducesResponseType(400)]
        public IActionResult Post(K id, [FromBody]T value)
        {
            _storage.Add(id.ToString(), value);
            return Created("", "");
        }
    }
}

loading the controllers

    public class PluginFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
    {
        private ControllerFeature feature;

        public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
        {
            this.feature = feature;
            DisablePreviousLoadedPlugins();
            ProcessAllPluginsInDirectory();
        }

which enables me to load the controller like so

feature.Controllers.Add(controllerType.MakeGenericType(new Type[] { item, keytype }).GetTypeInfo());

kind regards,

Frans

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions