Gab*_*Gab 5 c# asp.net-mvc asp.net-web-api swagger swashbuckle
我目前正在尝试在我的 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();
        }
    }
}
启动文件
[assembly: OwinStartupAttribute(typeof(WebApiExperiment.Startup))]
namespace WebApiExperiment
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}
全球.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.");
        }
    }
}
对于任何文件,请告诉我,我会为您提供。
简短回答:路由很重要。在不了解 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.");
        }
    }
}
| 归档时间: | 
 | 
| 查看次数: | 3200 次 | 
| 最近记录: |