2012-05-30 68 views
0

我建立與以下網址一個簡單的搜索引擎:ASP.NET MVC路由 - 它重定向的時候,我不希望它

"/" - this is the homepage with the search textbox and submission button. 
"https://stackoverflow.com/search?q=" - this is where the results will appear. The query is the q= parameter. 

這是我的搜索控制器:

[HandleError] 
public class SearchController : Controller { 

    public ActionResult Start() {   
     SearchModel model = new SearchModel(); 
     return View(model); 
    } 

    /// <summary>Performs a search.</summary> 
    /// <param name="q">The search query.</param> 
    /// <param name="a">The search action ("I Feel Lucky", etc).</param> 
    /// <param name="page">The results page number.</param> 
    public ActionResult Search(String q, String a, String page) { 
     return View(); 
    } 
} 

最後,這是我的路由表:

public static void RegisterRoutes(RouteCollection routes) {   
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapRoute(
     "SearchQuery", 
     "Search", // match "http://mysite.com/Search" 
     new { controller = "Search", action = "Search" } 
    ); 
    routes.MapRoute(
     "SearchStart", // Route name 
     "", // match "http://mysite.com/" 
     new { controller = "Search", action = "Start", id = UrlParameter.Optional } // Parameter defaults 
    ); 
} 

然而,當我做一個HTTP請求http://mysite.com/Search?q=foo我得到一個301重定向到http://mysite.com/Search/,其中404返回。我的SearchControllerSearch行爲從不被調用。

我需要做什麼來允許我在做什麼?

回答

0

沒有什麼可以導致這個問題。這是整個應用程序嗎?

MVC不會自行重定向。您必須告訴它重定向,或者使用某種在某些條件下重定向的屬性或過濾器。

+0

我改變了規則使用「/ S」而不是「/搜索」,它的工作原理。我在應用程序中刪除了每個對「搜索」的引用,出於某種原因,當我在地址欄中輸入時,仍然會看到301。我不知道它來自哪裏,但這是一個開始。 – Dai

+0

301是通常由瀏覽器緩存的永久性重定向。您可能在之前的某個時間將URL重定向,現在它被緩存。您是否嘗試清除瀏覽器的緩存? –

相關問題