Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Controller.Redirect(string)

I have a private function like this in my controller.

private UserDetails GetUserDetails(int userid)
{
    ...
    if (some condition check is false)
        Redirect("some other page in a different subdomain");

    return userDetails;
}

If the condition fails, Redirect statement is executed, but the execution doesn't stop. userDetails is returned to the calling function. There is no redirect to some other page.

How can I force the redirect?

like image 381
SKT Avatar asked Jan 24 '26 19:01

SKT


1 Answers

Redirect returns a result - you're meant to use it from within an Action method as follows:

public ActionResult MyAction() 
{
    if (check is false) 
    {
        return Redirect("other url");
    }
    // this code won't get executed after redirect
}

It looks like you should return null from the function you posted above, then check for null and return Redirect("...") if GetUserDetails returned null.

like image 163
Richard Avatar answered Jan 28 '26 13:01

Richard