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

MVC routing is using parameter as view name

下面添加了更多信息

当使用以下 "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

路由配置:

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 产品(字符串 SearchString,字符串布局 = "Grid") { ViewBag.Layout = 布局; return 查看(搜索字符串); }

更多信息

最终通过 "products" 操作并被处理的网址:

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

通过结合检查包括其区域在内的所有路线以及这些区域的路线注册是否正确,我解决了这个问题。