標籤: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頁 .