从Base继承时会招摇

时间:2018-09-07 19:14:47

标签: c# swagger asp.net-core-webapi swashbuckle

当前,我正在使用swashbuckle.AspNetCore.SwaggerUI 3.0,正在构建我的Web api,并且在我的控制器中,我是从基本控制器继承的。

public class DocumentTypeController : BaseController<ObjectNameGoesHere>
{
    public DocumentTypeController(IRepository repository) : base(repository)
    {
         //In here I will just inherit from the methods from the 
         //BaseController
    }
}

所有方法都在我可以重写的BaseController中。我收到这样的错误:

获取错误:内部服务器错误/swagger/v1/swagger.json。有什么想法可以解决此问题吗?

public abstract class BaseController<T> : Controller where T : class, IEntity
{

    protected IRepository _repo;

    public BaseController(IRepository repository)
    {
        _repo = repository;
    }


    [HttpGet]
    public virtual IQueryable<T> Get([FromServices] IQueryableRepository repository)
    {

        return repository.GetIQueryable<T>();

    }

    [HttpGet("{id:int}")]
    [Route("GetByID")]
    public async virtual Task<IActionResult> GetByIntID(int id)
    {
        try
        {
            var data = await _repo.GetByIdAsync<T>(id);
            return Ok(data);
        }
        catch (Exception exp)
        {
            return BadRequest("Bad Request");
        }
    }

    [HttpPost]
    [ApiExplorerSettings(IgnoreApi = true)]
    public async virtual Task<IActionResult> Post([FromBody] T dto)
    {
        try
        {
            if (ModelState.IsValid)
            {
                _repo.Create<T>(dto);
                var result = await _repo.SaveAndReturnOneAsync(dto);
                return CreatedAtAction("GetByIntID", new { id = result.Id }, result);
            }
            else
            {
                return BadRequest("Bad Request: Your data is incorrect");
            }
        }
        catch (Exception exp)
        {
            return BadRequest("Bad Request: Item Not Added");
        }
    }

    [HttpPut]
    [ApiExplorerSettings(IgnoreApi = true)]
    public async virtual Task<IActionResult> Put([FromBody] T dto)
    {
        try
        {
            if (ModelState.IsValid)
            {
                _repo.Update<T>(dto);
                await _repo.SaveAsync();
                return Accepted();
            }

            else
            {
                return BadRequest("Bad Request: Your data is incorrect");
            }

        }
        catch (Exception exp)
        {
            return BadRequest("Bad Request: Item not Updated");
        }
    }

}

0 个答案:

没有答案