設定ASP.NET MVC網站預設頁為.html頁 .

來源:互聯網
上載者:User

標籤:collect   參數   add   方法   預設路由   redirect   沒有許可權   static   自動   

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

設定ASP.NET MVC網站預設頁為.html頁 .

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.