ASP.NET WebAPI default landing page

2020-06-01 06:36发布

I've created a RESTful web service using ASP.NET WebApi v2 and I'm using Swashbuckle to generate swagger UI for API documentation.

All API calls are under http://localhost/api/ and the swagger UI page is at http://localhost/browser/index (the 'browser' part is configurable).

Browsing to http://localhost/ however will land on a empty page, so my question is is it possible to route http://localhost/ to http://localhost/browser/index so the user will be able to see the API documentation just by visiting the base uri.

One solution I can think of is to use a physical file system and create a static html page which does a meta fresh to redirect to the landing page I want, but I guess there must be a better way of doing this…

1条回答
别忘想泡老子
2楼-- · 2020-06-01 07:38

Change the route configuration RouteConfig.cs.

Instead of the default setting:

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

Change it to point at swagger:

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

Make sure you update RouteConfig.cs and not WebApiConfig.cs.

查看更多
登录 后发表回答