Swashbuckle 看不到我的任何控制器
Swashbuckle doesn't see any of my controllers
我有 ASP MVC 5 项目,我向其中添加了 Web Api。现在我正在尝试安装 Swashbuckle(来自 bootstrap)- 但它只显示一个没有任何控制器的空文档。
我的控制器:
[RoutePrefix("api/v1/Test/Check")]
public class TestController : ApiController
{
// GET: api/Test
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET: api/Test/5
[HttpGet]
public string Get(int id)
{
return "value";
}
// POST: api/Test
[HttpPost]
public void Post([FromBody]string value)
{
}
// PUT: api/Test/5
[HttpPut]
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Test/5
[HttpDelete]
public void Delete(int id)
{
}
// GET: api/Test
[Route("GetMetadata")]
[HttpGet]
public IEnumerable<string> GetMetadata()
{
return new string[] { "value2221", "value2" };
}
}
我的网站Api配置:
configuration.MapHttpAttributeRoutes();
configuration.Routes.MapHttpRoute("Public API V1", "api/v1/{controller}/{id}",
new { id = RouteParameter.Optional });
我的Global.asax.cs
:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
//WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Swashbuckle
配置为默认。
生成的 swagger 文档如下所示:
{
swagger: "2.0",
info: {
version: "v1",
title: "WebServer"
},
host: "localhost:55309",
schemes: [
"http"
],
paths: { },
definitions: { }
}
我可以在 http://localhost:55309/api/v1/Test
上访问我的控制器
我不确定我是否需要更改生成的 SwaggerConfig.cs
中的任何内容,但通过查看它和 swashbuckle
文档,它看起来应该无需任何修改就可以工作
我在我的 web api 项目中遇到了同样的问题,我是这样解决的:
1) 首先我创建了以下扩展方法:
public static class SwaggerExtensions
{
public static HttpConfiguration EnableSwagger(this HttpConfiguration httpConfiguration)
{
httpConfiguration
.EnableSwagger(c => c.SingleApiVersion("v1", "WebApi"))
.EnableSwaggerUi();
return httpConfiguration;
}
}
2) 然后在 Startup 里面 class:
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.EnableSwagger();
var webApiConfiguration = WebApiConfig.Register(config);
//here I commented other startup code
}
好的,我想我做到了。我不得不下载 swashbuckle
源并调试它,但这是值得的。
原来问题不是 swashbuckle
本身,而是 ApiExplorer
由于某种原因 .ApiDescriptions
为空。
在调试过程中,我将这两行放在 Application_Start()
的末尾,尽管这些行没有做任何事情,但奇迹发生了,它开始起作用了:
var explorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
var descriptions = explorer.ApiDescriptions;
然后我走得更远,找到了这个主题:ASP.Net Web API Help Page Area returning empty output(见第一个答案)
我使用的是 Glimpse
,尽管我实际上安装它是为了解决 swashbuckle
问题! (我认为它可能有帮助 - 它没有,但 Glimpse 感觉是一个不错的工具所以我把它留在那里)
正如第一个答案所暗示的,我修改了 web.config
为:
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<inspectors>
<ignoredTypes>
<add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet"/>
</ignoredTypes>
</inspectors>
</glimpse>
并从 Application_Start()
中删除了这两行,因为它们不是修复方法。它开始起作用了!
老实说,我不知道那里发生了什么。我清楚地记得在我开始使用 Glimpse
之前它没有工作,所以安装 glimpse 并修复其中的一个问题感觉不像是对原始问题的正确修复,但我真的厌倦了这个问题并且我准备像这样关闭它。
希望这对某人有所帮助。
P.S。只是对那些试图调试类似问题的人的警告。我的另一个错误是我在两次尝试之间没有关闭 IIS Express。它实际上保留了应用程序 运行 所以配置不是 re-applied 即使你在 VS 中 start/stop 应用程序。如果您使用配置 - 您需要在两次尝试之间关闭 ISS Express。
我有 ASP MVC 5 项目,我向其中添加了 Web Api。现在我正在尝试安装 Swashbuckle(来自 bootstrap)- 但它只显示一个没有任何控制器的空文档。
我的控制器:
[RoutePrefix("api/v1/Test/Check")]
public class TestController : ApiController
{
// GET: api/Test
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET: api/Test/5
[HttpGet]
public string Get(int id)
{
return "value";
}
// POST: api/Test
[HttpPost]
public void Post([FromBody]string value)
{
}
// PUT: api/Test/5
[HttpPut]
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Test/5
[HttpDelete]
public void Delete(int id)
{
}
// GET: api/Test
[Route("GetMetadata")]
[HttpGet]
public IEnumerable<string> GetMetadata()
{
return new string[] { "value2221", "value2" };
}
}
我的网站Api配置:
configuration.MapHttpAttributeRoutes();
configuration.Routes.MapHttpRoute("Public API V1", "api/v1/{controller}/{id}",
new { id = RouteParameter.Optional });
我的Global.asax.cs
:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
//WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Swashbuckle
配置为默认。
生成的 swagger 文档如下所示:
{
swagger: "2.0",
info: {
version: "v1",
title: "WebServer"
},
host: "localhost:55309",
schemes: [
"http"
],
paths: { },
definitions: { }
}
我可以在 http://localhost:55309/api/v1/Test
我不确定我是否需要更改生成的 SwaggerConfig.cs
中的任何内容,但通过查看它和 swashbuckle
文档,它看起来应该无需任何修改就可以工作
我在我的 web api 项目中遇到了同样的问题,我是这样解决的:
1) 首先我创建了以下扩展方法:
public static class SwaggerExtensions
{
public static HttpConfiguration EnableSwagger(this HttpConfiguration httpConfiguration)
{
httpConfiguration
.EnableSwagger(c => c.SingleApiVersion("v1", "WebApi"))
.EnableSwaggerUi();
return httpConfiguration;
}
}
2) 然后在 Startup 里面 class:
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.EnableSwagger();
var webApiConfiguration = WebApiConfig.Register(config);
//here I commented other startup code
}
好的,我想我做到了。我不得不下载 swashbuckle
源并调试它,但这是值得的。
原来问题不是 swashbuckle
本身,而是 ApiExplorer
由于某种原因 .ApiDescriptions
为空。
在调试过程中,我将这两行放在 Application_Start()
的末尾,尽管这些行没有做任何事情,但奇迹发生了,它开始起作用了:
var explorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
var descriptions = explorer.ApiDescriptions;
然后我走得更远,找到了这个主题:ASP.Net Web API Help Page Area returning empty output(见第一个答案)
我使用的是 Glimpse
,尽管我实际上安装它是为了解决 swashbuckle
问题! (我认为它可能有帮助 - 它没有,但 Glimpse 感觉是一个不错的工具所以我把它留在那里)
正如第一个答案所暗示的,我修改了 web.config
为:
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<inspectors>
<ignoredTypes>
<add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet"/>
</ignoredTypes>
</inspectors>
</glimpse>
并从 Application_Start()
中删除了这两行,因为它们不是修复方法。它开始起作用了!
老实说,我不知道那里发生了什么。我清楚地记得在我开始使用 Glimpse
之前它没有工作,所以安装 glimpse 并修复其中的一个问题感觉不像是对原始问题的正确修复,但我真的厌倦了这个问题并且我准备像这样关闭它。
希望这对某人有所帮助。
P.S。只是对那些试图调试类似问题的人的警告。我的另一个错误是我在两次尝试之间没有关闭 IIS Express。它实际上保留了应用程序 运行 所以配置不是 re-applied 即使你在 VS 中 start/stop 应用程序。如果您使用配置 - 您需要在两次尝试之间关闭 ISS Express。