ASP.NET MVC路由 - 当我不希望它重定向时

时间:2012-05-30 01:08:47

标签: asp.net-mvc url-routing asp.net-mvc-routing

我正在构建一个包含以下网址的简单搜索引擎:

"/" - this is the homepage with the search textbox and submission button.
"/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://mysite.com/Search?q=foo发出HTTP请求时,我会获得301重定向到http://mysite.com/Search/,其中返回404。我的SearchController。永远不会调用Search动作。

我需要做些什么来准备我正在做的事情?

1 个答案:

答案 0 :(得分:0)

您所展示的内容中没有任何内容会导致此问题。这是整个申请吗?

MVC本身不会重定向。您要么必须告诉它重定向,要么使用在某些条件下重定向的某种属性或过滤器。