Html.RenderAction未按预期命中默认路由

时间:2017-09-27 17:00:36

标签: asp.net asp.net-mvc routes

我有一个如下指定的默认路线:

routes.MapRoute(
            "Default",                                                  // Route name
            "{controller}/{action}/{id}",                               // URL with parameters
            new { controller = "Home", action = "Index", id = "", RouteName = "Default" },     // Parameter defaults
            new { controller = @"[^\.]*" }                              // Constraints (Ignore urls with periods in them)
        );

我有一个名为Test的控制器和一个名为DoSomething的测试操作,其定义如下:

public ActionResult DoSomething(int someId, int someotherId, IEnumerable<string> listOfSomething, bool flag, bool flag2)

我试图像这样调用这个动作:

        var parameters = new RouteValueDictionary();

        parameters.Add("someId", id);
        parameters.Add("someotherId", otherId);
        parameters.Add("flag", flag1);
        parameters.Add("flag2", flag2);
        for (int i = 0; i < thisList.Count; i++)
        {
            parameters.Add("listOfSomething[" + i + "]", thisList[i]);
        }
        Html.RenderAction("DoSomething", "Test", parameters);

Html.RenderAction调用失败并出现InvalidOperationException:路由表中没有路由与提供的值匹配。

这会导致什么?默认路由应该选择此调用吗?

1 个答案:

答案 0 :(得分:0)

路由表中的路由与提供的值不匹配表示RouteCollection中的有效路由与请求的URL不匹配,即您的默认路由参数计数与{{1不匹配动作参数(1个参数:5个参数)。

另请注意,DoSomething被视为复杂对象,因此您无法将其作为URL中IEnumerable<string>参数的一部分传递。因此,您应该只传递4个值参数&amp;将RouteValueDictionary对象作为IEnumerable<string>Session内容传递。

首先,在默认路由之上定义一个包含4个参数的自定义路由(避免修改在路由顺序中排在最后的默认路由,其他路由可能需要这样做):

TempData

然后,编辑控制器动作方法以接收4个参数(从参数列表中剥离routes.MapRoute( "Custom", // Route name "{controller}/{action}/{someId}/{someotherId}/{flag}/{flag2}", // URL with parameters new { controller = "Home", action = "Index", someId = "", someotherId = "", flag = "", flag2 = "" }, // Parameter defaults new { controller = @"[^\.]*" } // Constraints (Ignore urls with periods in them) ); ,基于上面的解释):

IEnumerable<string>

然后传递重定向参数,public class TestController : Controller { public ActionResult DoSomething(int someId, int someotherId, bool flag, bool flag2) { // other stuff } } 对象存储在IEnumerableTempData变量中:

Session

或直接在var parameters = new RouteValueDictionary(); parameters.Add("someId", id); parameters.Add("someotherId", otherId); parameters.Add("flag", flag1); parameters.Add("flag2", flag2); var list = new List<string>(); for (int i = 0; i < thisList.Count; i++) { list.Add(thisList[i]); } TempData["listOfSomething"] = list; Html.RenderAction("DoSomething", "Test", parameters);

中定义参数
RedirectToAction

如果var list = new List<string>(); for (int i = 0; i < thisList.Count; i++) { list.Add(thisList[i]); } TempData["listOfSomething"] = list; Html.RenderAction("DoSomething", "Test", new { someId = id, someotherId = otherId, flag = flag1, flag2 = flag2 }); 已经thisList,请删除IEnumerable<string>循环&amp;只需将其直接分配到for / Session

TempData

可以使用以下方式检索参数列表:

TempData["listOfSomething"] = thislist;

与参考相似的问题:

How to pass List in Redirecttoaction

Sending a list using RedirectToAction in MVC4

Passing an array or list of strings from one action to another when redirecting

Routing with Multiple Parameters using ASP.NET MVC

How to pass multiple objects using RedirectToAction() in Asp.NET MVC?