設置ASP.NET MVC站點默認頁爲html頁

設置ASP.NET MVC站點默認頁爲html頁 

標籤: 

mvc

 

默認頁

 

it

分類: C#

同事部署了一個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.PageBuildProvider" />
      </buildProviders>

  

方法3:

1)站點根目錄增加了default.html;

2)修改Global.asax默認的路由註冊,去掉默認controller:

routes.MapRoute(
                "Default", // 路由名稱
                "{controller}/{action}/{id}", // 帶有參數的 URL
                new {action = "Index", id = UrlParameter.Optional } // 參數默認值
            ); 

 

將iis中的默認文檔配置爲index.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章