Search code examples
asp.net-mvcasp.net-mvc-5redirecttoaction

Override RedirecToAction method


Is there a way to extend the RedirectToAction method on mvc 5 for accepts masterName parameter. I'm looking for something like this:

RedirectToAction(actionName: "Index", controllerName: controllerName, masterName: "_Layout");

Can you guys help me?


Solution

  • You can pass route values.

    return RedirectToAction(actionName: "Index", controllerName: controllerName,  new {masterName = "_Layout"});
    

    The action method should have a parameter with name masterName. This parameter will then receive the value given here. You can then inside the controller action pass on the parameter to the view.

    public ActionResult Index(string masterName)
    {
        // Other code
        return View("Index", masterName);
    }