MVC路由使用参数作为视图名称

时间:2015-06-12 06:02:51

标签: asp.net-mvc routes

下面添加了更多信息

使用以下" RedirectToAction" (产生以下URL)我得到:

**The view 'searchterm' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/browse/searchterm.aspx
~/Views/browse/searchterm.ascx
~/Views/Shared/searchterm.aspx
~/Views/Shared/searchterm.ascx
~/Views/browse/searchterm.cshtml
~/Views/browse/searchterm.vbhtml
~/Views/Shared/searchterm.cshtml
~/Views/Shared/searchterm.vbhtml**

生成请求的操作:

public ActionResult Action(string id)
{
    if (!(string.IsNullOrEmpty(id)))
    {
        string SearchTerm = id;
        return RedirectToAction("Products", new { SearchString = SearchTerm, Layout = "Row" });
    }
    return View();
}

URL:http://localhost:52006/browse/Products?SearchString=searchterm&Layout=Row

RouteConfig:

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 = "" });

    routes.MapRoute(
       "Admin", // Route name
       "Admin/{controller}/{action}/{id}", // URL with parameters
       new { controller = "Dashboard", action = "Index", id = "" });
}

产品控制器:     public ActionResult Products(string SearchString,string Layout =" Grid")     {         ViewBag.Layout =布局;         return View(SearchString);     }

更多信息

最终会通过"产品"行动和正在处理:

http://localhost:52006/Browse/Products?Layout=Row#

最终会查找名称为= SearchString的操作的网址:

http://localhost:52006/Browse/Products?Search=acolyte&Layout=Row - 很有意思,因为它表明参数的顺序不是抛弃路由/它不仅仅是抓住第一个参数作为要查找的动作。

http://localhost:52006/Browse/Products?Layout=Row&Search=acolyte

1 个答案:

答案 0 :(得分:-1)

通过检查所有路线包括他们的区域和区域的路线注册的组合是正确的我修复了这个问题。

相关问题