无法在Swagger UI

时间:2016-12-19 17:18:25

标签: c# .net asp.net-web-api swagger swashbuckle

我正在尝试设置我的web api项目以使用Swagger。

我安装了Swashbuckle,当我转到http://localhost:55010/swagger时Swagger UI工作,我看不到我的控制器动作。

enter image description here

我正在使用这种路径进行行动:http://localhost:55010/api/v1/Role

我目前只有一个版本的api,但我计划有多个版本,所以我在我的URL路径中使用v1(它是通过我的Controllers文件夹中的子文件夹设置的)。 / p>

以下是我去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();
        }
    }

    ...........

2 个答案:

答案 0 :(得分:1)

将招摇配置更改为:

^               # Match start of line
(?(?=■).*\n|.   # Test if first character is a ■. If not match one of anything
(?(?=■).*\n|.   # Test if second character is a ■. If not match one of anything
(?(?=■).*\n|.   # Test if third character is a ■. If not match one of anything
(?(?=■).*\n|.   # and so on...
(?(?=■).*\n|.   # and so on...
(?(?=■).*\n|.   # and so on...
(?(?=■).*\n|■   # Match a last ! or force a fail (match ■)
.).).).).).).)  # "On the way out" of the match, "eat" characters on the next line
(.).*$          # Capture the character and match the rest of the line

在所有其他配置之后配置它:

public class SwaggerConfig
{
    public static void Register(HttpConfiguration config)
    ...
}

答案 1 :(得分:0)

我在方法上删除了[AcceptVerbs(“ xxx”)],它们出现在我的Swagger中:-)