ASP.NET MVC搜索结果页面MapRoute不起作用

时间:2014-09-09 08:17:42

标签: asp.net-mvc maproute

如何为搜索结果页面设置mapRoute?我的代码不起作用。

的Global.asax.cs

 routes.MapRoute(
           name: "SearchResults",
           url: "{action}/{Keyword}",
           defaults: new { controller = "Home", action = "Search" }
       );

搜索表单

@using (Html.BeginForm("Search", "Home", FormMethod.Get))
            {
                @Html.TextBox("Keyword",null , new { @class = "SearchBox" })
                <input type="submit" value="Search" />
            }

HomeController.cs

public ActionResult Search(string Keyword)
    {
        GamesContext db = new GamesContext();
        var SearchResults= (from i in db.Games where i.GameName.Contains(Keyword) || i.GameDesc.Contains(Keyword) select i).Take(20).ToList();

        return View(SearchResults.AsEnumerable());
    }

1 个答案:

答案 0 :(得分:0)

这个适合我(应该在默认路线之前):

routes.MapRoute(
     "SearchResults",
     "Search/{Keyword}",
     new { controller = "Search", action = "SearchAction" }
);

Creating an ActionLink and MapRoute that there is a constant name in it

并且有一点是使用新的控制器进行搜索,而不是使用这条路线回家。