【问题标题】:With asp.net MVC4, how can I make my root index.html be executed by default?使用 asp.net MVC4,我怎样才能让我的根 index.html 默认执行?
【发布时间】:2013-01-18 19:56:20
【问题描述】:

对于我的大部分网站,我希望正常路由以 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 }
        );
    }

【问题讨论】:

标签: asp.net-mvc-4 asp.net-mvc-routing


【解决方案1】:
    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();
    }

希望这会有所帮助!

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

      为所有控制器定义路由并删除默认根:

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

      新的默认值为 index.html。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-27
        • 1970-01-01
        • 2015-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多