2009-04-27 122 views
13

我正在使用IIS 6.我認爲我的問題是我不知道如何使用routes.MapRoute路由到非控制器。ASP.NET MVC路由在html頁面啓動

我有一個像example.com這樣的url,我希望它能夠服務於index.htm頁面,而不是使用MVC。我該如何設置?在IIS中,我將index.htm作爲我的開始文檔,而我的global.asax具有標準的「默認」路由,它將其稱爲Home/Index。

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.MapRoute(
      "Default",            // Route name 
      "{controller}/{action}/{id}",       // URL with parameters 
      new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
     ); 

    } 

我加了這一點:

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     if (Context.Request.FilePath == "/") Context.RewritePath("index.htm"); 
    } 

它的工作原理。但這是最好的解決方案嗎?

+0

我覺得有趣的是,您指出您正在運行IIS6。我不禁想到這將是解決方案中的關鍵因素 – 2009-04-27 19:09:45

回答

-1

routes.IgnoreRoute?

而且,看到了這個問題:How to ignore route in asp.net forms url routing

+0

我添加了這個: routes.Add(new Route(「{resource} .htm/{* pathInfo}」,new StopRoutingHandler())) ; 它沒有工作。 添加了: routes.IgnoreRoute(「{resource} .htm/{* pathInfo}」);也沒有工作 – Marsharks 2009-04-27 19:02:53

+0

你添加了什麼命令?我認爲重要的是StopRoutingHandler路線將成爲列表中的第一個。 – 2009-04-27 20:47:55

18

我加了一個虛擬控制器中指定的Web站點的根目錄中時,作爲默認的控制器使用。此控制器具有單個索引操作,可以重定向到根目錄中的index.htm網站。

public class DocumentationController : Controller 
{ 
    public ActionResult Index() 
    { 
     return Redirect(Url.Content("~/index.htm")); 
    } 

} 

請注意,我正在使用這是一個基於MVC的REST Web服務的文檔。如果您轉到網站的根目錄,您將獲得該服務的文檔,而不是某種默認的Web服務方法。

-1

IIS6與IIS7在工作方式上有幾點不同。請查看Phli Haack的blog post關於如何使用ASP.NET MVC的設置。祝你好運!

6

配置asp.net路由忽略root ("/") requests,讓IIS's "Default Document" ISAPI篩選器服務於靜態index.htm文件

添加以下到RegisterRoutes方法。

routes.IgnoreRoute(""); 
1

最好的解決方案是刪除默認的控制器。你遇到了這個問題,因爲你指定了默認頁面和沒有任何參數的默認路由。

通過只刪除航線上默認的controller = "Home",該/將不再匹配的路由,因爲沒有其他途徑滿足,IIS將查找到默認文檔。

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapRoute(
     "Default",            // Route name 
     "{controller}/{action}/{id}",       // URL with parameters 
     new { action = "Index", id = "" }      // Parameter defaults 
    ); 
}