【问题标题】:How to set Swagger as default start page?如何将 Swagger 设置为默认起始页?
【发布时间】:2018-05-07 13:09:41
【问题描述】:

如何将 Swagger 设置为在 ABP 模板中而不是 /Account/Login 的默认起始页?

我正在使用 ASP.NET MVC 5.x + Angular 1.x。

更新

当前代码:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //ASP.NET Web API Route Config
    routes.MapHttpRoute(
        name: "swagger_root",
        routeTemplate: "",
        defaults: null,
        constraints: null,
        handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

一切仍然正常,除了模块零的"api/Account/Authenticate" 请求已损坏,显示:

找不到资源。

【问题讨论】:

标签: c# angularjs asp.net-mvc-5 swagger-ui aspnetboilerplate


【解决方案1】:

在 Asp .Net Web Api Core 3.x 中只需这样做(RoutePrefix):

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Baha'i Prayers API");
            c.InjectStylesheet("/swagger/custom.css");
            c.RoutePrefix = String.Empty;
        });

【讨论】:

    【解决方案2】:

    我知道主要问题是针对 ASP.Net MVC,但对于 ASP.Net Core 来说,对于类似问题(使用 SwaggerUI)的这个答案是一个很好的解决方案:https://stackoverflow.com/a/50127631/1179562

    app.UseSwaggerUI(c =>
    {
        c.RoutePrefix = "";
        ...
    };
    

    【讨论】:

    • 这样更简单更好。只是因为其他提供的解决方案需要我们对路由进行配置设置。请记住,如果有任何提供,您还需要从 launchSettings.json 中删除 launchUrl,否则这将不起作用。
    【解决方案3】:

    在 Visual Studio、IIS Express 和 IIS 中对我来说效果很好。它是创建一个具有以下内容的控制器:

    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    
    namespace WebApplication.Controllers
    {
        /// <summary>
        /// Controller to display API documentation in Swagger format
        /// </summary>
        [Route("")]
        [ApiExplorerSettings(IgnoreApi = true)]
        public class DocsController : Controller
        {
            [Route("docs"), HttpGet]
            [AllowAnonymous]
            public IActionResult ReDoc()
            {
                return View();
            }
    
            [Route(""), HttpGet]
            [AllowAnonymous]
            public IActionResult Swagger()
            {
                return Redirect("~/swagger");
            }
        }
    }
    

    注意:编辑 launchsettings.json 文件在 Visual Studio 中运行良好,但坚持认为在 IIS 上托管应用程序时它无法按预期运行。

    通过这种方式,我发现它比在多个不同位置创建大量配置更简洁。

    【讨论】:

      【解决方案4】:

      我转到解决方案资源管理器面板 > 属性。在那里我找到了一个名为launchsettings.json 的文件。

      在此文件中,我在找到它的所有部分中将“launchUrl”参数的值更改为“swagger/index.html”。

      这对我有用。

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 我刚更新到AspNet Core 3.1,上面解决方案中的正斜杠是不需要的。我不确定这是特定于我的配置还是一般情况?
        • 这就是您使用程序 x 执行此操作的方式。如果您正在使用程序 A 怎么办?骑士?如果您直接从代码中工作怎么办?
        【解决方案6】:

        我可以通过使用特定页面> /swagger/ui/index 填写项目属性的开始操作来做到这一点

        注意:我正在使用来自 NuGet 的 Swashbuckle

        【讨论】:

        • 它可以工作,但会将 url 保存在用户特定的文件中。因此,每个开发人员都必须手动完成。
        【解决方案7】:

        RouteConfig.cs 中添加this 路由,此处已注释掉:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        
            //ASP.NET Web API Route Config
            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
        
            // Set Swagger as default start page
            /*
            routes.MapHttpRoute(
                name: "swagger_root",
                routeTemplate: "",
                defaults: null,
                constraints: null,
                handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));
            */
        
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
        

        【讨论】:

          猜你喜欢
          • 2023-01-19
          • 1970-01-01
          • 2018-02-23
          • 2016-04-21
          • 2015-04-13
          • 1970-01-01
          • 2014-05-04
          • 1970-01-01
          • 2015-11-27
          相关资源
          最近更新 更多