MVC4.0 如何设置默认静态首页index.shtml

1.不启用二级域名情况下(www.xxx.com)下设置默认静态首页index.shtml

  通过配置IIS的默认文档,设置默认首页地址

  

  然后在MVC的路由中写入忽略默认路由代码

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.IgnoreRoute("");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new string[] { "XXX.WebUI.Controllers" }//解决Controllers与Areas中控制器不能同名问题
    );

 2.启用二级域名情况下(xxx.xxx.com)下设置默认静态首页index.shtml

  实现接口IRouteConstraint接口

    public class DomainXXXXConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return httpContext.Request.Url.Host.ToLowerInvariant() == "XXXX";
        }
    }

  然后在MVC的路由中写入忽略默认路由代码

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

        routes.IgnoreRoute("", new { DomainConstraint = new DomainXXXXConstraint() })

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "XXX.WebUI.Controllers" }//解决Controllers与Areas中控制器不能同名问题
        );
    }

 

posted @ 2016-01-18 11:24  荒古禁地  阅读(2125)  评论(0编辑  收藏  举报