将index.html设置为默认页面

时间:2014-03-26 09:43:11

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

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

我试图右键单击index.html并设置为起始页面,当我运行它时,网址为: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 }
            );
        }
    }

5 个答案:

答案 0 :(得分:30)

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

routes.IgnoreRoute(""); 

答案 1 :(得分:20)

当@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

答案 2 :(得分:6)

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

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

答案 3 :(得分: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 }
        );

答案 4 :(得分:3)

这是一个解决方案:

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

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

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

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