从继承自定义基本控制器类继承时,使用swagger解析JSON / YAML响应失败

时间:2017-11-27 23:56:00

标签: c# swagger-ui asp.net-core-2.0

我收到错误,无法解析JSON / YAML响应。仅当我的控制器继承自BaseController类时才会发生这种情况。如果我删除BaseController它工作正常。

[Route("v1/helloworld")]
    public class ValuesController : BaseController
    {
        private SystemManager _mgr { get; }
        public ValuesController(SystemManager mgr):base(mgr)
        {
            _mgr = mgr;
        }
        // GET: api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }

BaseController.cs

public abstract class BaseController : Controller
    {
        public SystemManager SysMgr { get; }

        public LogWriter Logger
        {
            get { return SysMgr.Logger; }
        }

        public BaseController(SystemManager sysMgr)
        {
            SysMgr = sysMgr;
        }

        public async Task LogAsync(Exception ex)
        {
            await LogAsync(JsonConvert.SerializeObject(ex, Formatting.Indented));
        }

        public async Task LogAsync(string message)
        {
            var le = new LogEntry()
            {
                Message = message
            };
            await Logger.LogAsync(le);
        }

        public string GetUsername() => User.Claims.Where(a => a.Type == "Username").FirstOrDefault().Value;
        public string GetIPAddress() => HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress?.ToString() ?? string.Empty;
    }

1 个答案:

答案 0 :(得分:0)

通过在基本控制器类中将所有方法设置为protected来修复。