带有 ASP.NET MVC WebApi 的 Swashbuckle 5.4.0 - swagger 网页内未显示任何文档
Swashbuckle 5.4.0 with ASP.NET MVC WebApi - No documentation is shown inside the swagger webpage
我目前正在尝试在我的 ASP.NET MVC Web API 应用程序中使用 Swashbuckle 5.4.0。每当我转到 swagger 生成的页面(通过 rooturl/swagger)时,页面都会正确加载,但未显示任何方法。将我的项目与另一个正常工作的项目进行比较,我可以发现代码中的任何差异。我错过了什么?
这是与问题相关的 .cs 文件:
SwaggerConfig.cs:
namespace WebApiExperiment
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "Swagger Demo Api");
c.IncludeXmlComments(string.Format(@"{0}\bin\WebApiExperiment.XML",
System.AppDomain.CurrentDomain.BaseDirectory));
})
.EnableSwaggerUi();
}
}
}
Startup.cs
[assembly: OwinStartupAttribute(typeof(WebApiExperiment.Startup))]
namespace WebApiExperiment
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
Global.asax
namespace WebApiExperiment
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
HomeController.cs(我项目中最简单最笨的控制器)
namespace WebApiExperiment.Controllers
{
public class HomeController : ApiController
{
public IHttpActionResult Index(SendCodeViewModel sendView)
{
return Ok();
}
public IHttpActionResult About(SendCodeViewModel sendView)
{
return Ok("Your application description page.");
}
public IHttpActionResult Contact(SendCodeViewModel sendView)
{
return Ok("Your contact page.");
}
}
}
对于任何文件,请告诉我,我会提供给您。
简答:路由很重要。 swagger 如何在不了解路由的情况下生成文档 API?
试试这个:
namespace WebApiExperiment.Controllers
{
[RoutePrefix("api/routingMatters")]
public class HomeController : ApiController
{
[Route("magic")]
public IHttpActionResult Index(SendCodeViewModel sendView)
{
return Ok();
}
[Route("magic2")]
public IHttpActionResult About(SendCodeViewModel sendView)
{
return Ok("Your application description page.");
}
[Route("magic3")]
public IHttpActionResult Contact(SendCodeViewModel sendView)
{
return Ok("Your contact page.");
}
}
}
我目前正在尝试在我的 ASP.NET MVC Web API 应用程序中使用 Swashbuckle 5.4.0。每当我转到 swagger 生成的页面(通过 rooturl/swagger)时,页面都会正确加载,但未显示任何方法。将我的项目与另一个正常工作的项目进行比较,我可以发现代码中的任何差异。我错过了什么?
这是与问题相关的 .cs 文件:
SwaggerConfig.cs:
namespace WebApiExperiment
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "Swagger Demo Api");
c.IncludeXmlComments(string.Format(@"{0}\bin\WebApiExperiment.XML",
System.AppDomain.CurrentDomain.BaseDirectory));
})
.EnableSwaggerUi();
}
}
}
Startup.cs
[assembly: OwinStartupAttribute(typeof(WebApiExperiment.Startup))]
namespace WebApiExperiment
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
Global.asax
namespace WebApiExperiment
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
HomeController.cs(我项目中最简单最笨的控制器)
namespace WebApiExperiment.Controllers
{
public class HomeController : ApiController
{
public IHttpActionResult Index(SendCodeViewModel sendView)
{
return Ok();
}
public IHttpActionResult About(SendCodeViewModel sendView)
{
return Ok("Your application description page.");
}
public IHttpActionResult Contact(SendCodeViewModel sendView)
{
return Ok("Your contact page.");
}
}
}
对于任何文件,请告诉我,我会提供给您。
简答:路由很重要。 swagger 如何在不了解路由的情况下生成文档 API?
试试这个:
namespace WebApiExperiment.Controllers
{
[RoutePrefix("api/routingMatters")]
public class HomeController : ApiController
{
[Route("magic")]
public IHttpActionResult Index(SendCodeViewModel sendView)
{
return Ok();
}
[Route("magic2")]
public IHttpActionResult About(SendCodeViewModel sendView)
{
return Ok("Your application description page.");
}
[Route("magic3")]
public IHttpActionResult Contact(SendCodeViewModel sendView)
{
return Ok("Your contact page.");
}
}
}