简体   繁体   English

RedirectToAction MVC 5上的ActionExecutingContext.ActionParameters NULL-T4MVC

[英]ActionExecutingContext.ActionParameters NULL on RedirectToAction MVC 5 - T4MVC

I have a redirection like: 我有一个重定向,如:

return RedirectToAction(MVC.Area.Controller.Index(institutionId));

This redirects to a controller which inherits from another controller which has an action filter defined like so: 这将重定向到一个从另一个控制器继承的控制器,该控制器具有如下定义的动作过滤器:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var institutionId = filterContext.ActionParameters["institutionId"];

    var institution = InstitutionRepository.GetById((int)institutionId);

    ...other code
}

the ActionExecutingContext.ActionParameters contain the institutionId but the value is always NULL. ActionExecutingContext.ActionParameters包含机构ID,但该值始终为NULL。 I've tried redirecting to route as well but that gives the same output. 我也尝试过重定向到路由,但这给出了相同的输出。 What bothers me is that when I look at the route, the institutionId seems to be passed with query parameters (Area/Controller?institutionId=17 instead of Area/Controller/17) 令我困扰的是,当我查看路线时,似乎使用查询参数传递了机构ID(Area / Controller?institutionId = 17而不是Area / Controller / 17)

MVC stands for T4MVC, which is framework used to avoid hardcoding strings in the redirects. MVC代表T4MVC,它是用于避免在重定向中对字符串进行硬编码的框架。

If anyone at some point might encounter it, it's actually quite tricky. 如果某个时候有人遇到它,那实际上是很棘手的。 It seems in some cases T4MVC confuses views and interprets: 在某些情况下,T4MVC似乎使视图和解释混乱:

return RedirectToAction(MVC.Area.Controller.Index(institutionId));

as

return RedirectToAction(MVC.Area.OtherController.Index(institutionId));

Looking at the ActionParameters, the collection had both Controller and OtherController in the routeData so it seems it just looked up the Controller name by the View name (Index), found two instances belonging to both Controllers and generated a broken MVC redirection string. 查看ActionParameters,该集合在routeData中同时包含Controller和OtherController,因此似乎只是按视图名称(索引)查找Controller名称,找到了属于两个Controller的两个实例,并生成了损坏的MVC重定向字符串。 After I changed the name of the View, the ActionParameters started resolving correctly. 更改视图的名称后,ActionParameters开始正确解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM