With asp.net MVC4, how can I make my root index.ht

2020-07-10 11:56发布

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 }
        );
    }

3条回答
地球回转人心会变
2楼-- · 2020-07-10 12:14
    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!

查看更多
贼婆χ
3楼-- · 2020-07-10 12:21

Define the routes for all your controllers and remove the default root:

            name: "Default",
            url: "{controller}/{action}/{id}",

The new default will be index.html.

查看更多
太酷不给撩
4楼-- · 2020-07-10 12:23

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.

查看更多
登录 后发表回答