如何从根URL重定向到/ swagger / ui / index?

时间:2016-06-09 11:26:47

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

我有一个安装了Swashbuckle的WebApi项目。

在默认设置中,我必须在浏览器http://localhost:56131/swagger/ui/index中打开才能查看我的操作说明和测试页面。我希望可以从网站的根目录访问它:http://localhost:56131/。我怎样才能做到这一点?

8 个答案:

答案 0 :(得分:27)

this answer to similar question影响,稍加修改的代码:

public class WebApiConfig
{
    public static void Configure(IAppBuilder app)
    {
        var httpConfig = new HttpConfiguration();

        // Attribute routing
        config.MapHttpAttributeRoutes();

        // Redirect root to Swagger UI
        config.Routes.MapHttpRoute(
            name: "Swagger UI",
            routeTemplate: "",
            defaults: null,
            constraints: null,
            handler: new RedirectHandler(SwaggerDocsConfig.DefaultRootUrlResolver, "swagger/ui/index"));

        // Configure OWIN with this WebApi HttpConfiguration
        app.UseWebApi(httpConfig);
    }
}

这样就没有必要像@bsoulier那样创建新的WebAPI控制器。

此解决方案基于RedirectHandler程序集中已存在的类Swashbuckle.Core

答案 1 :(得分:8)

如果您确定使用Web API项目,则可以:

[Route(""), HttpGet]
[ApiExplorerSettings(IgnoreApi = true)]
public HttpResponseMessage RedirectToSwaggerUi()
{
    var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Found);
    httpResponseMessage.Headers.Location = new Uri("/swagger/ui/index", UriKind.Relative);
    return httpResponseMessage;
}

请注意使用ApiExplorerSettings属性,以便它不会出现在Swagger定义中。

答案 2 :(得分:2)

上述答案的更简单的变体:

public class DefaultController : Controller
{
    [Route(""), HttpGet]
    [ApiExplorerSettings(IgnoreApi = true)]
    public RedirectResult RedirectToSwaggerUi()
    {
        return Redirect("/swagger/");
    }
}

越简单越好!这个对我有用。

答案 3 :(得分:0)

如果任务允许,请在_layout.chtml中添加以下行:

<meta http-equiv="refresh" content="0; URL='/swagger'" />

这将在页面加载后重定向到大张旗鼓/索引

答案 4 :(得分:0)

对于属性launchsettings.json中的.net core,修改配置文件和api部分以指向招摇。

profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "authapi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/swagger",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }

答案 5 :(得分:0)

在asp.net core 2.1 / 2.2的launchSetting.Json中更改launchUrl = "swagger/index.html"的值

"ProjectName": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:44333"
    }

答案 6 :(得分:0)

对于ASP Net Core> 2.2中的RESTFUL API,请在Project / Properties / Debug中设置默认URL

enter image description here

然后launchSettings.json将自动更新:

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },

答案 7 :(得分:-1)

如果您运行的是dotnet core,只需转到properties-> launchsettings.json并注释掉或删除launchUrl,然后将大胆的RoutePrefix设置为string.Empty。