【问题标题】:ASP.NET WebAPI default landing pageASP.NET WebAPI 默认登陆页面
【发布时间】:2016-02-26 18:43:05
【问题描述】:

我已经使用 ASP.NET WebApi v2 创建了一个 RESTful Web 服务,并且我正在使用 Swashbuckle 为 API 文档生成 Swagger UI。

所有 API 调用都在 http://localhost/api/ 下,而招摇的 UI 页面在 http://localhost/browser/index(“浏览器”部分是可配置的)。

浏览到http://localhost/ 但是会出现在一个空白页面上,所以我的问题是是否可以将http://localhost/ 路由到http://localhost/browser/index,这样用户就可以通过访问基本 uri 来查看 API 文档。

我能想到的一个解决方案是使用物理文件系统并创建一个静态 html 页面,该页面执行元新鲜以重定向到我想要的登录页面,但我想必须有更好的方法来做到这一点......

【问题讨论】:

  • 不能在控制器中做重定向,从默认页面到swagger页面吗?
  • @KarlGjertsen 我想我可以为此创建一个新控制器并路由默认页面..是否可以在没有专用控制器的情况下实现这一目标?

标签: asp.net asp.net-mvc asp.net-web-api asp.net-web-api2


【解决方案1】:

更改路由配置RouteConfig.cs。

代替默认设置:

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

改成指向招摇:

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

确保更新RouteConfig.cs 而不是WebApiConfig.cs。

【讨论】:

  • 考虑相同的场景。但我必须重定向到“localhost/browser/index.html”静态 html 文件。他们有机会成功吗?
  • 我的 Swagger URL:localhost:49952/swagger/ui/index 我更改了 RouteConfig.cs 文件,如下所示: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}",默认值:new { controller = "Swagger", action = "Index", id = UrlParameter.Optional } );
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 2018-03-04
  • 2020-05-25
  • 1970-01-01
  • 2012-03-18
  • 2017-08-18
  • 1970-01-01
相关资源
最近更新 更多