我有一个 web api 2 项目。它被配置为使用 owin 自托管。它没有任何 global.asax 文件。我需要有 web api 的帮助页面,并为此使用了 swaschbuckle。但是 rooturl/swagger/docs 没有给出任何输出。
我已按照此处的说明“ https://github.com/domaindrivendev/Swashbuckle/issues/196 ”,但它仍然无法正常工作。下面是我的配置代码
public void Configuration(IAppBuilder app)
{
// Configure DI
container = BuildDI();
// Create the configuration. OWIN Should create a new httpconfiguration.
// GlobalConfiguration can't be used.
HttpConfiguration = new HttpConfiguration();
HttpConfiguration.Formatters.XmlFormatter.UseXmlSerializer = true;
HttpConfiguration.MapHttpAttributeRoutes();
HttpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// Set ServicePointManager properties.
ServicePointManager.ServerCertificateValidationCallback = ((sender, cert, chain, sslPolicyErrors) => true);
// 10 concurrent connections can be made on the service point.
ServicePointManager.DefaultConnectionLimit = 10;
// After the idle time expires, the ServicePoint object is eligible for
// garbage collection and cannot be used by the ServicePointManager object.
ServicePointManager.MaxServicePointIdleTime = 30000; // 30 Seconds.
app.UseSerilogRequestContext("RequestId");
// Middleware is injected form DI.
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(HttpConfiguration);
//Enable swashbuckle
SwaggerConfig.Register(HttpConfiguration);
// Webapi middleware. Do it at the end.
app.UseWebApi(HttpConfiguration);
// Register callback to dispose container.
RegisterShutdownCallback(app, container);
}
public class SwaggerConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableSwagger(c =>
{
c.RootUrl(rurl => ConfigurationManager.AppSettings["hostUrl"].ToString());
c.IncludeXmlComments(GetXmlCommentsFileLocation());
c.SingleApiVersion("v1", "Isone");
})
.EnableSwaggerUi(c =>
{
});
}
private static string GetXmlCommentsFileLocation()
{
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\bin";
var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML";
var commentsFileLocation = Path.Combine(baseDirectory, commentsFileName);
return commentsFileLocation;
}
}
如果代码中有错误,请指出。