C# 我怎样才能变得狂妄自大;虚张声势地显示端点?

C# 我怎样才能变得狂妄自大;虚张声势地显示端点?,c#,asp.net-core,swagger,swagger-ui,swashbuckle,C#,Asp.net Core,Swagger,Swagger Ui,Swashbuckle,我正在尝试将swagger+Swashback添加到我的ASP.NET核心项目中。我可以让大摇大摆的用户界面启动并运行,但它是完全空的。我试着四处看看,发现了一个类似的问题。这让我觉得可能是路由问题,所以我尝试在方法上使用[route(“testroute”)]为控制器提供一个显式路由,而不是类。这使得我添加的端点显示为没有问题的路由 由于向每个端点添加显式路由是非最优的,我做错了什么?如何修复它以使swagger显示我的所有端点 我的初创公司整合了swagger public clas

我正在尝试将swagger+Swashback添加到我的ASP.NET核心项目中。我可以让大摇大摆的用户界面启动并运行,但它是完全空的。我试着四处看看,发现了一个类似的问题。这让我觉得可能是路由问题,所以我尝试在方法上使用
[route(“testroute”)]
为控制器提供一个显式路由,而不是类。这使得我添加的端点显示为没有问题的路由

由于向每个端点添加显式路由是非最优的,我做错了什么?如何修复它以使swagger显示我的所有端点

我的初创公司整合了swagger

    public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
                        .SetBasePath(env.ContentRootPath)
                        .AddJsonFile("appsettings.json", optional: true , reloadOnChange: true);

        Configuration = builder.Build();
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });

        services.AddDbContext<PromotionContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:Jasmine"]));
        services.AddTransient<PromotionDbInitializer>();
        services.AddTransient<IComponentHelper, ComponentHelper>();
        services.AddTransient<IComponentFileHelper, ComponentFileHelper>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, PromotionDbInitializer promotionSeeder)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });



        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Promotion}/{action=Index}/{id?}");
        });

       //Because there is not a seed method built into the EF migrations pipeline in EFCore this seeding method will interfere with the migrations when attempting to deploy the database
       //uncomment if you need to seed

       //promotionSeeder.Seed().Wait();
    }
}
试着改变

public class PromotionController : Controller


将路由属性添加到控制器:

[Route("[controller]/[action]")]
public class PromotionController : Controller
{
...
并在操作上设置HttpGet属性:

[HttpGet]
public IActionResult GetAll()
{
...

[HttpGet("{promoCode}")]
public IActionResult Get(string promoCode)
{
...

您必须小心如何混合和匹配静态和动态路由。查看有关asp.net core中基于属性的路由的更多详细信息。

这是asp.net core,因此不再有ApiController,mvc和webapi库已经合并,只有Controller类可以工作。我通读了这篇文章,但我不明白它为什么奏效。据我所知,这会将我在启动中设置的路由移动到类和方法的属性中。你知道为什么Swashback/swagger不能处理默认的路由吗?实际上我不确定Swashback是否正确地处理生成文档的默认MVC路由。我倾向于使用属性路由和XML文档来生成我的招摇过市ui。
[Route("[controller]/[action]")]
public class PromotionController : Controller
{
...
[HttpGet]
public IActionResult GetAll()
{
...

[HttpGet("{promoCode}")]
public IActionResult Get(string promoCode)
{
...