I have a WebAPI application that is used for some RESTful operations in the database. It works great, but I want to match a route with the root URL. For example, I want to go to the root of the site and see some useful information that are dynamically generated.
Currently, I have it setup so that it follows the standard convention of api/{controller}/{action}, but how can I show this information when navigated to the root instead of something like api/diagnostics/all?
Basically what I want is, when the user navigates to the root URL, I want to route that request to TestController.Index()
I have the following setup in the WebApiConfig.cs file:
public static void Register(HttpConfiguration config)
{
    // Web API routes
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
        name: "Index",
        routeTemplate: "",
        defaults: new { controller = "Test", action = "Index" }
    );
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional, controller = "Test" }
    );
}
And this is what my TestController.cs looks like:
[RoutePrefix("api")]
public class TestController : ApiController
{
    [Route("TestService"), HttpGet]
    public string Index()
    {
         return "Service is running normally...";
    }        
}
                You could also simply use ([Route("")]):
public class TestController : ApiController
{
    [Route(""), HttpGet]
    public string Index()
    {
         return "Service is running normally...";
    }        
}
                        You can add a route for the default URL in WebApiConfig.cs. Here's an example where the root URL is mapped to method HomeController.Index():
config.Routes.MapHttpRoute(
    name: "Root",
    routeTemplate: "",   // indicates the root URL
    defaults: new { controller = "Home", action = "Index" }   // the controller action to handle this URL
);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With