ASP.NET WebAPI default landing page

Godsent picture Godsent · Nov 24, 2015 · Viewed 24.9k times · Source

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…

Answer

Karl Gjertsen picture Karl Gjertsen · Nov 24, 2015

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.