我正在尝试将我的web api项目设置为使用Swagger。
我安装了Swashbuckle,当我转到http://localhost:55010/swagger
时,Swagger UI可以正常工作,但我看不到控制器的任何操作。
我在我的操作中使用了这种路径:http://localhost:55010/api/v1/Role
我目前只有一个版本的api,但我计划有多个版本,所以我在URL路径中使用v1
(它是通过我的控制器文件夹中的子文件夹设置的)。
下面是我访问http://localhost:55010/swagger/docs/v1
时看到的内容
{"swagger":"2.0","info":{"version":"v1","title":"Swashbuckle Dummy API V1"},"host":"localhost:55010","schemes":["http"],"paths":{},"definitions":{}}
这是我正在使用的配置:
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.MultipleApiVersions(
(apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
(vc) =>
{
//vc.Version("v2", "Swashbuckle Dummy API V2");
vc.Version("v1", "Swashbuckle Dummy API V1");
});
})
.EnableSwaggerUi(c =>
{
});
}
private static bool ResolveVersionSupportByRouteConstraint(ApiDescription apiDesc, string targetApiVersion)
{
// I don't know how I am supposed to use this
return true;
}
}
我的路由配置:
config.Routes.MapHttpRoute(
name: "WithActionApi",
routeTemplate: "api/{folder}/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { action = @"[A-Za-z]+" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{folder}/{controller}/{id}",
defaults: new { action = "DefaultAction", id = RouteParameter.Optional }
);
还有一个控制器的例子:
public class GridTemplateController : BaseController
{
GridTemplateLogic logic;
public GridTemplateController(IPermissionValidator permissionValidator, IRolePermissionLogic logicRolePermission)
: base(permissionValidator, logicRolePermission)
{
logic = new GridTemplateLogic(new GridTemplateRepository(ConnectionString, CurrentUser), permissionValidator);
}
// GET: api/v1/GridTemplate/ForGrid/5
[HttpGet]
public IHttpActionResult ForGrid(int id)
{
try
{
var entityList = logic.GetAllByGridId(id);
return Ok(new ApiResponse(entityList));
}
catch (UnauthorizedAccessException)
{
return Unauthorized();
}
}
...........
发布于 2017-05-31 11:02:46
将swagger配置更改为:
public class SwaggerConfig
{
public static void Register(HttpConfiguration config)
...
}
并在完成所有其他配置后进行配置:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
...
SwaggerConfig.Register(config);
}
发布于 2019-05-24 15:21:28
我删除了方法上的AcceptVerbs("xxx"),它们出现在我的Swagger中:-)
https://stackoverflow.com/questions/41227782
复制