Pectus Excavatum
Pectus Excavatum

Reputation: 3773

.Net MVC4 redirect to action not working as expected

This is probably a silly question, but it seems as though RedirectToAction() isnt working correctly.

I have an Admin Controller done as a separate area, and I have a logout task method, and I am simply trying to get the method to redirect to the website index on logout, but it is taking me to a url under the admin route instead and I have no idea why as I am calling the method giving it the controller name and action method name inside that controller. instead it takes me to: http://localhost:63374/Admin/Home

Is there something I am doing wrong?

        [Route("logout")]
    public async Task<ActionResult> Logout()
    {
        var authManager = HttpContext.GetOwinContext().Authentication;

        authManager.SignOut();

        return RedirectToAction("index", "home");
    }

Upvotes: 4

Views: 4540

Answers (2)

Tommy
Tommy

Reputation: 39807

When not specified, the route values are persisted between calls. Since you are in the admin area and not specifying an area in your RedirectToAction method, the route engine is keeping the reference to the admin area.

To solve this, you can either:

  • Move your logout action outside of the admin area or,
  • Specify that the RedirectToAction should find a controller outside of the current area by setting the area to an empty string.

return RedirectToAction("Index", new{controller="Home", area=string.Empty});

Upvotes: 7

Andrew
Andrew

Reputation: 1534

Try to use return RedirectToAction("Index", "Home");. As i know, all these Redirect* and RedirectTo* methods are case sensitive

Upvotes: 1

Related Questions