C# 招摇过市未知响应类型

C# 招摇过市未知响应类型,c#,.net-core,C#,.net Core,我试图通过Swagger API获得简单的JSON结果,但我看不到JSON的主体。我收到错误“未知响应类型” 仪表板控制器.cs Web.API.Controllers { [Produces("application/json")] [Route("api/dashboards")] public class DashboardController : Controller { private readonly DbContext contex

我试图通过Swagger API获得简单的JSON结果,但我看不到JSON的主体。我收到错误“未知响应类型”

仪表板控制器.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);
        }
    }
}
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();
        }
    }
}
Web.API.Controllers
{
[产生(“应用程序/json”)]
[路线(“api/仪表盘”)]
公共类仪表板控制器:控制器
{
私有只读DbContext上下文;
公共仪表板控制器(DbContext上下文)
{
this.context=上下文;
}
/// 
///请在此处描述此方法。
/// 
[HttpGet]
公共异步任务GetDashboard(int id)
{
var entity=await context.Dashboards
.包括(d=>d.DashboardType)
.SingleOrDefaultAsync(d=>d.Id==Id);
返回Ok(实体);
}
}
}
有人知道出了什么问题吗?我已经在控制器上手动添加了[products(“application/json”)],测试了Mozzila和Chrome等

Startup.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);
        }
    }
}
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();
        }
    }
}
namespace Web.API
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“默认”));
services.AddMvc();
services.AddSwaggerGen(c=>
{
c、 SwaggerDoc(“v1”,新信息{Title=“测试API”,Description=“Swagger核心测试API”});
var xmlPath=System.AppDomain.CurrentDomain.BaseDirectory+@“Test.Web.API.xml”;
c、 includexmlcoments(xmlPath);
});
services.AddTransient();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void Configure(IApplicationBuilder应用程序、IHostingEnvironment环境、DatabaseDeployer部署器)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c=>
{
c、 SwaggerEndpoint(“/swagger/v1/swagger.json”,“核心API”);
});
}
app.Run(异步(上下文)=>
{
wait context.Response.WriteAsync(“Hello World!”);
});
app.UseMvc();
Migrate();
}
}
}

那么Swagger如何知道您的方法返回什么呢

或者确保它可以通过方法声明找到您返回的内容:

[HttpGet]
public async Task<DashBoardData> GetDashboard(int id)
[HttpGet]
公共异步任务GetDashboard(int id)
或者,如果您想保留IActionResult,至少要注释它将是什么:

[HttpGet]
[SwaggerResponse(200, typeof(DashBoardData), "Successful Request.")]
public async Task<IActionResult> GetDashboard(int id)
[HttpGet]
[SwaggerResponse(200,typeof(仪表盘数据),“请求成功”。]
公共异步任务GetDashboard(int id)

假设
DashBoardData
是查询返回的类。

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

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

当我删除它时,我得到了json响应结果,效果很好

你有没有拿过像Fiddler这样的工具来看看实际返回的是什么?