如何从 root url 重定向到 /swagger/ui/index?

How to redirect from root url to /swagger/ui/index?

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

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

如果您确定使用 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 定义中。

影响,略微修改代码:

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 控制器。

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

上述答案的更简单变体:

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

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

如果任务允许,在 _layout.chtml 中只需在标题中添加以下行:

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

这将在页面加载后 swagger/index 重定向您

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

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"
      }

更改 launchUrl = "swagger/index.html" in launchSetting.Json in asp.net core 2.1/2.2

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

如果您是 运行 dotnet 核心,只需转到属性->launchsettings.json 并注释掉或删除 launchUrl 并将 swagger RoutePrefix 设置为 string.Empty。

对于 RESTFUL API in ASP Net Core > 2.2, ,在 Project/Properties/ Debug

中设置默认值 URL

并且launchSettings.json会自动更新:

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