Reputation: 209
The index action on my home controller is the entry point for my MVC 3 app. When I try to redirect to another view from within this action i'm getting the 'child actions are not allowed to perform redirect actions' error on an Html.Action on my layout view. I'm not redirecting from a child view so can anyone explain why I'm getting this error. This only happens on the Index action of my home controller, any other action on any other controller, including the home controller works fine.
Home controller:
public class HomeController : Controller
{
public ActionResult Index()
{
if ([some logic to lookup user] = 0)
return View("UserNotFound");
else
return view();
}
}
@Html.Action in layout:
@Html.Action("BuildMenu", "Menu", new { menu = @ViewBag.Menu })
Global.asax:
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 = UrlParameter.Optional } // Parameter defaults
);
}
Menu controller:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
[ChildActionOnly]
public ActionResult BuildMenu(string menu)
{
var model = new ViewModels.MenuViewModel()
{
Menu = menu,
ShowCreateNew = BL.Permissions.CheckUserPermission(this.UserDetails.UserId, this.UserDetails.RoleId, BL.Common.permission_AddNewRisk),
ShowAdmin = BL.Permissions.CheckUserPermission(this.UserDetails.UserId, this.UserDetails.RoleId, BL.Common.permission_AdminRights)
};
return PartialView("_Menu", model);
}
Upvotes: 1
Views: 1466
Reputation: 35793
Is there a redirect inside the BuildMenu
action of the Menu
controller that only occurs when the user is not found?
That would cause this error as that is the child action referenced.
I have just tested exactly this and got the error:
Child actions are not allowed to perform redirect actions
If the this.UserDetails
value is null, which based on the condition in the Home Index I assume is possible then that would cause an exception which is possibly being caught by a globally applied attribute which contains a redirect.
I tested this as well and got the same error.
Upvotes: 3