2016-08-30 104 views
0

我有一个OWIN,自托管的Web API项目,我想将Swagger文档和Swagger UI添加到它。Swashbuckle - 从BaseController继承的Web API控制器未显示

我已经包含了Swashbuckle.Core软件包,并且我已经手动配置它,编号为Startup.cs

configuration.EnableSwagger(s => 
{ 
    s.SingleApiVersion(Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace('.', ' '), "MyApi").; 
    s.IncludeXmlComments([email protected]"{System.AppDomain.CurrentDomain.BaseDirectory}\MyApi.XML"); 
    s.DescribeAllEnumsAsStrings(); 
}) 
.EnableSwaggerUi(s => 
{ 
    // By default, swagger-ui will validate specs against swagger.io's online validator and display the result 
    // in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the 
    // feature entirely. 
    //c.SetValidatorUrl("http://localhost/validator"); 
    s.DisableValidator(); 
}); 

现在,我有一个基础控制器和两个附加的控制器,从基地继承。不幸的是,我没有看到控制器的名字和行动在狂欢的页面。

这里是我的基本控制器:

public class BaseController : ApiController 
{ 
    public BaseController() 
    { 
     // Initialization... 
    } 

    public async Task<IHttpActionResult> MyAction() 
    { 
     // Code here... 
    } 
} 

Controler1:

public class My1Controller : BaseController 
{ 
    public MyController1(...): base(...) 
    { 
     // Initialization... 
    } 

    public async Task<IHttpActionResult> Post(Model1 model) 
    { 
     // Code here... 
    } 

    public async Task<IHttpActionResult> Post(Model2 model) 
    { 
     // Code here... 
    } 
} 

控制器2:

public class My2Controller : BaseController 
{ 
    public My2Controller(...): base(...) 
    { 
     // Initialization... 
    } 

    public async Task<IHttpActionResult> Post(Model1 model) 
    { 
     // Code here... 
    } 

    public async Task<IHttpActionResult> Post(Model2 model) 
    { 
     // Code here... 
    } 
} 

我看不到My1Controller也不招摇索引页My2Controller。我试过My1ControllerMy2Controller上的ApiExplorerSettings(IgnoreApi = true)]属性,但没有发生任何事情。

是否因为控制器操作共享通用名称(具有不同参数类型的多个Post操作)?我没有像上面的例子那样使用RPC风格的URL,而是遵循5级媒体类型(5LMT)提案的RESTful URL。

有什么建议吗?

+0

是您的问题帽子看不到从基本控制方法,或者你没有看到基地和子控制器方法? – bsoulier

+0

@bsoulier正确,我在swagger文档页面中看不到这两个文件 – gdyrrahitis

回答

0

的ASP.NET Web API与naming conventions工作,这意味着你的控制器不解析,因为他们没有自己的名字与Controller结束。

这意味着MyController1变成My1Controller

因此,作为一个例子,改变MyController1到下面的工作:

public class My1Controller : BaseController 
{ 
    public MyController1(...): base(...) 
    { 
     // Initialization... 
    } 

    public async Task<IHttpActionResult> Post(Model1 model) 
    { 
     // Code here... 
    } 

    public async Task<IHttpActionResult> Post(Model2 model) 
    { 
     // Code here... 
    } 
} 
+0

这仅仅用于演示,控制器遵循命名约定。更新问题,因为它似乎误导。 – gdyrrahitis

+0

没有pb,下次提供更真实的样品 – bsoulier