简体   繁体   English

.Net MVC4重定向到操作不按预期工作

[英].Net MVC4 redirect to action not working as expected

This is probably a silly question, but it seems as though RedirectToAction() isnt working correctly. 这可能是一个愚蠢的问题,但似乎RedirectToAction()无法正常工作。

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 相反它需要我: 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");
    }

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. 由于您位于管理区域而未在RedirectToAction方法中指定区域,因此路由引擎将保留对管理区域的引用。

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. 通过将区域设置为空字符串,指定RedirectToAction应在当前区域之外找到控制器。

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

Try to use return RedirectToAction("Index", "Home"); 尝试使用return RedirectToAction("Index", "Home"); . As i know, all these Redirect* and RedirectTo* methods are case sensitive 据我所知,所有这些Redirect*RedirectTo*方法都区分大小写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM