我目前正在尝试在我的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.");
}
}
}
对于任何文件,请告诉我,我会为您提供。
答案 0 :(得分:1)
简短回答:路由很重要。如果没有API中的路由知识,swagger如何生成文档?
试试这个:
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.");
}
}
}