【问题标题】:Set index.html as the default page将 index.html 设置为默认页面
【发布时间】:2014-05-04 14:47:07
【问题描述】:

我有一个空的 ASP.NET 应用程序并添加了一个 index.html 文件。我想将 index.html 设置为网站的默认页面。

我试图右键单击 index.html 并设置为起始页,当我运行它时,url 是:http://localhost:5134/index.html 但我真正想要的是当我输入:http://localhost:5134 时,它应该加载index.html 页面。

我的路线配置:

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

【问题讨论】:

  • 你的动作“索引”在哪个控制器上?是在“家”控制器吗?
  • 我在应用程序中只有一个控制器:公共类 MainController:控制器
  • 好的,所以试着把defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }改成defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }
  • 我不知道这是否区分大小写,如果真正的操作名称是'index',请小心action = "Index"
  • 这个网址“localhost:5134”是否仍然呈现索引页面?如果是,那么您需要在解决方案>属性>Web>开始操作>特定页面中设置起始页

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


【解决方案1】:

我在路由配置中添加了一条指令以忽略空路由,这解决了我的问题。

routes.IgnoreRoute(""); 

【讨论】:

  • 谢谢!在这个上浪费了两个小时。
  • 上面的答案对我来说在本地有效,但是当我将它部署到生产环境时,我开始在 Post 请求中使用不允许的方法。
  • 这很奇怪,因为它在没有这个的情况下在 iisexpress 中工作......关于为什么它在 IIS8 中不同并且需要这个有什么想法吗?
【解决方案2】:

正如@vir 回答的那样,将routes.IgnoreRoute(""); 添加到RegisterRoutes(RouteCollection routes),默认情况下您应该在RouteConfig.cs 中找到它。

下面是这个方法的样子:

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

原因是 ASP.NET MVC 接管了 URL 管理,并且默认情况下路由是这样的,所有无扩展 URL 都由 web.config 中定义的无扩展 URL 处理程序控制。

有详细解释here

【讨论】:

  • 感谢您提供详细信息!很好的解释。
  • 谢谢。这只是一个有效的。应该是最佳答案。为什么不对所有内容使用 .cshtml mvc 视图?因为其他人在使用 Visual Studio 的 .net 开发人员以外的网站上工作。商业设计师更喜欢维护自己的营销 html 页面,因为这样对每个人来说效果最好。
【解决方案3】:

假设 web 应用在 IIS 中运行,默认页面可以在 web.config 文件中指定:

<system.webServer>
    <defaultDocument>
        <files>
            <clear />
            <add value="index.html" />
        </files>
    </defaultDocument>
</system.webServer>

【讨论】:

  • 在 MVC 中,其他答案是必需的,但这也是必需的。
【解决方案4】:

创建一个新的控制器 DefaultController。在索引操作中,我写了一行重定向:

return Redirect("~/index.html")

在 RouteConfig.cs 中,更改路由的 controller="Default"。

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
        );

【讨论】:

  • 这是一个非常糟糕的主意。 index.html 的巨大好处是加载速度。上面将首先访问服务器,经历整个服务器请求周期,直到它到达那个重定向,然后最后告诉浏览器去加载 .html 导致不必要的延迟。理想情况下,您希望将您的 Web 服务器配置为默认为 index.html,而无需访问任何 .net 资源,直到它们可以做一些有用的事情。
【解决方案5】:

一个解决方案是这个:

 //routes.MapRoute(
 //           name: "Default",
 //           url: "{controller}/{action}/{id}",
 //           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
 //      );

我的意思是,在您的 MVC 项目中注释或删除此代码,以避免在您发出初始请求 http://localhost:5134/ 时出现默认行为。

index.html 必须位于解决方案的根目录中。

希望这会有所帮助!它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多