Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: Asp.net WebApi default route to index.html

I am trying to create an Asp.net WebApi / Single Page Application. I would like my server to dispense index.html if no route is given. I would like it to use a controller when one is specified in the normal "{controller}/{id}" fashion.

I realized that I can visit my index page by using http://localhost:555/index.html. How do I do the same by visiting http://localhost:555 ?

like image 525
Phillip Scott Givens Avatar asked Jul 02 '16 23:07

Phillip Scott Givens


1 Answers

Just add a route to your WebApiConfig file for index page. Your method should look like this:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Attribute routing
            config.MapHttpAttributeRoutes();

            // Route to index.html
            config.Routes.MapHttpRoute(
                name: "Index",
                routeTemplate: "{id}.html",
                defaults: new {id = "index"});

            // Default route
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
like image 183
Hernan Guzman Avatar answered Sep 20 '22 11:09

Hernan Guzman