Swagger未知的响应类型

时间:2018-03-27 10:36:24

标签: c# .net-core

我'我试图通过Swagger API获得简单的JSON结果,但我看不到JSON的主体。我收到错误"未知的回复类型"

enter image description here

DashboardController.cs

Web.API.Controllers
{
    [Produces("application/json")]
    [Route("api/dashboards")]
    public class DashboardController : Controller
    {
        private readonly DbContext context;

        public DashboardController(DbContext context)
        {
            this.context = context;
        }

        /// <summary>
        /// Describe this method here. 
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetDashboard(int id)
        {
            var entity = await context.Dashboards
                .Include(d => d.DashboardType)
                .SingleOrDefaultAsync(d => d.Id == id);
            return Ok(entity);
        }
    }
}

有人知道问题是什么吗?我手动在Controller上添加了[Produces(&#34; application / json&#34;)],测试了Mozzila和Chrome以及同样的东西。

Startup.cs

namespace Web.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));

            services.AddMvc();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "Test API", Description ="Swagger core test API" });
                var xmlPath = System.AppDomain.CurrentDomain.BaseDirectory + @"Test.Web.API.xml";
                c.IncludeXmlComments(xmlPath);
            });

            services.AddTransient<DatabaseDeployer>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, DatabaseDeployer deployer)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Core API");
                });
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
            app.UseMvc();
            deployer.Migrate();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

那么Swagger将如何知道你的方法返回什么?

要么确保它能通过方法声明找出你返回的内容:

[HttpGet]
public async Task<DashBoardData> GetDashboard(int id)

或者如果你想保留IActionResult,至少注释它会是什么:

[HttpGet]
[SwaggerResponse(200, typeof(DashBoardData), "Successful Request.")]
public async Task<IActionResult> GetDashboard(int id)

假设DashBoardData是从您的查询返回的类。

答案 1 :(得分:-1)

我已经解决了这个问题。问题在于Startup.cs文件中的这部分

  app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });

当我删除它时,我得到了json响应结果并且工作正常。