Swashbuckle 5找不到我的ApiControllers。

21 浏览
0 Comments

Swashbuckle 5找不到我的ApiControllers。

我现在非常需要我的WebAPI 2项目的API文档,所以我使用了Swashbuckle 5 NuGet包。开箱即用,我可以访问{myrooturl}/swagger,并弹出一个UI界面,但是里面没有控制器、方法或者其他任何东西。只有我的标题:[基本URL:/EM.Services,API版本:v1]

我查看了Swashbuckle文档,并且由于我使用的是由IIS托管的OWIN,所以我根据这个文档修改了SwaggerConfig:

c.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));

根据这个文档: https://github.com/domaindrivendev/Swashbuckle/blob/1326e753ce9b3a823b3c156b0b601134692ffc58/README.md#transitioning-to-swashbuckle-50

我还设置了项目的构建以生成XML文档,并将我的SwaggerConfig指向它:

    private static string GetXmlCommentsPath()
    {
        // tried with an without the \bin
        return String.Format(@"{0}\bin\EM.Services.XML", AppDomain.CurrentDomain.BaseDirectory);
    }

不过我不确定XML文档是否起作用与否,因为在swagger-ui页面上我完全看不到任何控制器。

值得一提的是,我所有的控制器都继承自一个BaseController,而BaseController又继承自ApiController。

我的WebApiConfig是否有什么问题呢?

    public static void Register(HttpConfiguration config)
    {
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        config.Filters.Add(new ValidateModelAttribute());
        config.Filters.Add(new BaseAuthenticationAttribute());
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        var jsonFormatter = config.Formatters.OfType().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    }

我的具体控制器都是这样的(我已经尝试将BaseController替换为ApiController,但没有任何变化):

[RoutePrefix("api/whatever")]
public class FooController : BaseController

而我的Base控制器并不做太多事情(至少不现在),只有一个属性:

[BuildClaims]
public abstract class BaseController : ApiController

无论是使用IIS Express还是完整版的IIS,空白页面都还存在。

更新:

这是我创建的一个简单控制器的示例,它非常基本。它也没有显示出来,因为我仍然在swagger ui中看到了空白页面。

/// 
/// I am a test
/// 
[RoutePrefix("api/dummy")]
public class DummyController : ApiController
{
    [HttpGet]
    [Route("foo")]
    public int Foo()
    {
        return 42;
    }
}

0