使用asp.net MVC4,我怎样才能使我的root index.html默认执行?

Pet*_*ner 6 asp.net-mvc-routing asp.net-mvc-4

对于我的大部分网站,我希望正常路由以MVC方式发生.但是,当应用程序首次启动时,我不希望路由转到/Home/Index.cshtml.我希望它简单地转到/Index.html

我目前的RegisterRoutes看起来像这样(并没有实现我的目标)

    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 }
        );
    }
Run Code Online (Sandbox Code Playgroud)

小智 7

    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();
    }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


jls*_*dez 1

我不知道你是否考虑到这一点,在根 web.config 中你应该设置:

<appSettings>
    <add key="webpages:Enabled" value="true" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

如果根目录中有一个index.cshtml(当然该文件只能包含html代码),则RouteConfig.cs中不需要任何其他内容。

但是,如果您只想提供(而不是处理)文件,我的意思是,例如index.html,您还需要将此文件设置为项目中的起始页面,这就是最简单的答案。