设置ASP.NET MVC站点默认页为.html页 .

同事部署了一个Asp.Net MVC的站点,希望它的默认页是index.html页,在vs2010中给站点根目录增加了index.html,然后调用没有什么问题,但部署到IIS7上,在功能试图=》默认文档添加了index.html,但是只输入域名还是访问不到,看来还是.net mvc和IIS不兼容的原因,后来同事采用的办法是在global文件中把默认页面写成一个需要登录的页面,这样因为没有权限,系统会自动跳转到登录页面

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

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "IndexPage", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
        }

朋友找到了一个很好的博文,感觉实现方法更加灵活,具体如下:

方法1:

在Global.asax文件中增加

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Context.Request.FilePath == "/") Context.RewritePath("index.html");
}

方法2:

新建一个路由DefaultController,并把它设置为默认路由,在Action中增加

Redirect(Url.Content("~/index.html"));

此方法需要修改web.config配置

在Web.config文件中的<compilation>节点中增加:

     <buildProviders>
        <add extension=".htm" type="System.Web.Compilation.

原文地址:https://www.cnblogs.com/webenh/p/asp_net__mvc__html.html