2017-10-30 16 views
0

私はASP.NET Coreプロジェクトにswagger + swashbuckleを追加しようとしています。スワッガーUIを起動して実行することはできますが、それは完全に空です。私は周りを突きつけてみたが、同様の問題がhttps://github.com/domaindrivendev/Swashbuckle/issues/1058で見つかった。これはルーティングが問題であったと思うように思ったので、コントローラーにメソッドの上で[Route("testroute")]を使って明示的なルートを与えようとしましたが、クラスではありません。これにより、エンドポイントが問題なく表示されるようにルートを追加しました。Swagger + Swashbuckleにエンドポイントを表示するにはどうすればよいですか?

すべてのエンドポイントに明示的なルートを追加するのは最適ではないため、私は間違っています。どのようにしてすべてのエンドポイントを表示するように修正しますか?

闊歩がGETALLとPostメソッドがtestrouteとtestFormRoute下闊歩UIページ上に表示されますが、取得及び削除方法は

まで表示されません

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 
{ 
    private PromotionContext context; 
    public PromotionController(PromotionContext _context) 
    { 
     context = _context; 
    } 

    public IActionResult Index() 
    { 
     return View(); 
    } 

    [HttpGet] 
    [Route("testroute")] 
    public IActionResult GetAll() 
    { 
     try 
     { 
      var result = context.Promotions 
          .Include(promotion => promotion.CombinabilityType) 
          .Include(promotion => promotion.ValueType) 
          .Include(promotion => promotion.Currency) 
          .Include(promotion => promotion.Components) 
           .ThenInclude(component => component.TargetType) 
          .ToList(); 
      return Ok(result); 
     } 
     catch(Exception ex) 
     { 
      return StatusCode(500); 
     } 
    } 


    public IActionResult Get(string promoCode) 
    { 
     try 
     { 
      var result = context.Promotions 
           .Include(promotion => promotion.CombinabilityType) 
           .Include(promotion => promotion.ValueType) 
           .Include(promotion => promotion.Currency) 
           .Include(promotion => promotion.Components) 
            .ThenInclude(component => component.TargetType) 
           .FirstOrDefault(x => x.PromoCode == promoCode); 
      return Ok(result); 
     } 
     catch(Exception ex) 
     { 
      return StatusCode(500); 
     } 
    } 

    [HttpPost] 
    [Route("testFormRoute")] 
    public IActionResult Post([FromForm] Promotion newPromotion) 
    { 
     try 
     { 
      context.Promotions.Add(newPromotion); 
      context.SaveChanges(); 
     } 
     catch(DbUpdateException ex) 
     { 
      return StatusCode(500); 
     } 

     return Ok(); 
    } 

    [HttpDelete] 
    public IActionResult Delete(string promoCode) 
    { 
     try 
     { 
      var promotion = context.Promotions.FirstOrDefault(x => x.PromoCode == promoCode); 

      if(promotion != null) 
      { 
       context.Promotions.Remove(promotion); 
       context.SaveChanges(); 
      } 
     } 
     catch(DbUpdateException ex) 
     { 
      return StatusCode(500); 
     } 

     return Ok(); 
    } 
} 

答えて

2

あなたのコントローラにルート属性を追加します:あなたは、静的をミックスし、一致する方法について注意する必要があり

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

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

[Route("[controller]/[action]")] 
public class PromotionController : Controller 
{ 
... 

をそして、あなたの行動にHTTPGET属性を設定しますダイナミックルート。 asp.netコアの属性ベースルーティングの詳細については、this articleを参照してください。

+0

しか機能しませんでした。私は記事を読んで、なぜそれがうまくいったのか見ていない。私が知る限り、これはスタートアップで設定したルートをクラスとメソッドの属性に移します。スワッシュバック/スワッガーがデフォルトのルーティングを処理できない理由を知っていますか? –

+0

Swashbuckleがドキュメントを生成するためのデフォルトのMVCルーティングを適切に処理するかどうかは、実際はわかりません。私は、XML文書と組み合わせた属性ルーティングを使用して、魅力的なUIを生成する傾向があります。 – Nathan

-1

変更してみてください

public class PromotionController : Controller 

から

public class PromotionController : ApiController 
+0

これはASP.NET Coreであり、ApiControllerがなくなり、mvcライブラリとwebapiライブラリがマージされ、コントローラクラス –

関連する問題