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

给你一囗甜甜゛ 提交于 2019-12-04 05:48:44
    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!

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!