Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to Action from within a method

Tags:

c#

asp.net-mvc

In most of the Actions in my controller I need to check to see if a condition has been meet, and if not redirect the user to another Action in the same controller so I tried:

    public ActionResult Transactions()
    {
        GrossGalsConfermation();
        return View();
    }

    public void GrossGalsConfermation()
    {
        if (Session["HasConfirmed"] == null && TerminalUserData.IsGrossGallonTerminal)
        {
            RedirectToAction("ConversionFactors");
        }
    }

but even when the if statement is true the RedirectToAction is not executed. I have done a debug in VS and can see it jump from the Action to the Method, evaluate the IF statement and go into it to run the RedirectToAction but then it jumps right back to the calling Acation and return's it's view. How can I force it to redirect? Searching on line I find similar problems and the solution was to change from void to ActionResult but then I have to provide an fallback Action to return which can not be done since this Method will be called by dozens of views.

like image 495
Matthew Verstraete Avatar asked Dec 27 '13 21:12

Matthew Verstraete


People also ask

Which method is used to redirect to another action method?

To redirect the user to another action method from the controller action method, we can use RedirectToAction method.

What is difference between RedirectToAction and RedirectToRoute?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Save this answer. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .

How redirect to action in view in MVC?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.

Which return type is used to redirect to an action method of another controller?

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.


2 Answers

All RedirectToAction does is return a RedirectToRouteResult - the browser won't be redirected unless you actually return it. Since you don't want to do this within your method (you could return null and do null checks in your action but it would be clumsy), I'd suggest using an action filter instead. Something like the following would do it:

public class GrossGalsFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (Session["HasConfirmed"] == null
                                    && TerminalUserData.IsGrossGallonTerminal)
        {
            filterContext.Result =
                new RedirectToRouteResult(new RouteValueDictionary(new
                { controller = "MyController", action = "ConversionFactors" }));
        }

        base.OnActionExecuting(filterContext);
    }
}

Then decorate your controller actions:

[GrossGalsFilter]
public ActionResult MyControllerAction

Note that you may have to modify the filter to get hold of TerminalUserData.IsGrossGallonTerminal - impossible to know where this comes from based on your question.

More on action filters: http://msdn.microsoft.com/en-us/library/dd381609(v=vs.100).aspx

like image 171
Ant P Avatar answered Oct 02 '22 21:10

Ant P


You are missing a "return":

return RedirectToAction(...);

If you want to reuse the method multiple times you should look into writing a filter.

like image 35
Z.D. Avatar answered Oct 02 '22 19:10

Z.D.