Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With asp.net MVC4, how can I make my root index.html be executed by default?

For much of my web site, I want normal routing to occur the MVC way. However, when the app first launches, I don't want the route to go to /Home/Index.cshtml. I want it to go to simply /Index.html

My current RegisterRoutes looks like this (and does not achieve my goal)

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("index.html");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
like image 961
Peter Kellner Avatar asked Jan 18 '13 19:01

Peter Kellner


People also ask

What is default route in asp net?

The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.

What is route Config CFG in ASP.NET MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.

What are the main feature of ASP.NET MVC 4 used by ASP Net Web API?

ASP.NET MVC 4 includes ASP.NET Web API, a new framework for creating HTTP services that can reach a broad range of clients including browsers and mobile devices. ASP.NET Web API is also an ideal platform for building RESTful services.


2 Answers

    public ActionResult Index() {
        string FilePath = Server.MapPath("~/index.html");
        // You can add other conditions also here
        if (System.IO.File.Exists(FilePath)) {
            return File(FilePath, "text/html");
        }
        return View();
    }

Hope this helps!

like image 60
Binu Pararath Avatar answered Sep 28 '22 08:09

Binu Pararath


i don't know if you take this into account, in the root web.config you should set:

<appSettings>
    <add key="webpages:Enabled" value="true" />
</appSettings>

you don't need anything else in the RouteConfig.cs if you have a index.cshtml in the root (this file can contain only html code ofcourse).

But if you want to serve (not process) the file only, i mean, index.html for example you need also to set up this file as start Page in the project and thats all, simpliest answer.

like image 30
jlsfernandez Avatar answered Sep 28 '22 09:09

jlsfernandez