MVC4.0 如何設置默認靜態首頁index.shtml

1.不啓用二級域名狀況下(www.xxx.com)下設置默認靜態首頁index.shtmlhtml

  經過配置IIS的默認文檔,設置默認首頁地址url

  

  而後在MVC的路由中寫入忽略默認路由代碼spa

    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.shtml3d

  實現接口IRouteConstraint接口htm

    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的路由中寫入忽略默認路由代碼blog

    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中控制器不能同名問題
        );
    }
相關文章
相關標籤/搜索