gpt4 book ai didi

c# - 带有 ASP.NET MVC WebApi 的 Swashbuckle 5.4.0 - swagger 网页内未显示文档

转载 作者:行者123 更新时间:2023-11-30 12:40:55 25 4
gpt4 key购买 nike

我目前正在尝试在我的 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(我项目中最简单和最笨的 Controller )

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.");
}
}
}

关于c# - 带有 ASP.NET MVC WebApi 的 Swashbuckle 5.4.0 - swagger 网页内未显示文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com