我一直试图弄清楚为什么Swagger-Net不会在控制器中显示 endpoints 方法 .
C#项目使用基于.Net framework 4.6.1的Web API模板 .
当我使用SwashBuckler时,我得到了相同的结果,因此不是Swagger-Net就是问题,而是没有配置或丢失的东西 .
SwaggerConfig看起来像这样
public class SwaggerConfig
{
/// <summary>
///
/// </summary>
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", nameof(ConsentResponseApp));
c.AccessControlAllowOrigin("*");
c.IncludeAllXmlComments(thisAssembly, AppDomain.CurrentDomain.BaseDirectory);
})
.EnableSwaggerUi(c =>
{
c.UImaxDisplayedTags(100);
c.UIfilter("''");
});
}
}
我现在处于死胡同,因为我不知道为什么Swagger无法读取方法动作名称 .
答案:
默认情况下,WebApiConfig路由未配置为使用操作进行路由
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
它必须改为
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
2 回答
您需要一个路线,而不是一个动作名称
除此之外,建议添加预期的响应,如下所示:
以莱昂的评论为基础 . 您需要指定上面Leon所示的路线 .
我不确定
[ActionName()]
是否是您所需要的,因为它将允许您的API使用者指定带有.NET可能不允许的字符的URI或使用与实际控制器方法不同的签名 .请参阅this帖子,了解
[ActionName()]
背后的原因 .