José Machacaz
José Machacaz

Reputation: 21

RedirectToAction in static method

Maybe i'm having just a brain meltdown ..

but i had this static method (in a static class) and the ideia was to redirectoaction(s). when called.

    public static class ControllerHelpers
{
    public static ActionResult RedirectToLogon(HttpContext ctx) 
    {
        try
        {
            return View(@"Account\LogOn.aspx", new { ReturnUrl = ctx.Request.RawUrl });
        }
        catch (Exception)
        {
        }
        finally 
        {
            ctx = null;
        }
    }

}

Can someone help on this? I keep having this: "The name 'View' does not exist in the current context"

Upvotes: 2

Views: 1649

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

That is not a redirect. What you want is something like this, which is an actual 301 redirect which will cause the URL to change in the browser:

public static class ControllerHelpers
{
    public static ActionResult RedirectToLogon(HttpContext ctx) 
    {
        try
        {
            return new RedirectToRouteResult(
                new RouteValueDictionary(
                   new { 
                       action = "LogOn", 
                       controller = "Account", 
                       ReturnUrl = ctx.Request.RawUrl }));
        }
        catch (Exception)
        {
        }
        finally 
        {
            ctx = null; // Why on earth do you want to do this????
        }
    }
}

Upvotes: 2

Related Questions