【问题标题】:MVC Default root without areaMVC 默认根无区域
【发布时间】:2017-08-07 14:50:08
【问题描述】:

我正在尝试在 MVC 中使用区域。如果登录成功,您将被重定向到Admin 区域中的HomeController 中的Index。我想要做的是将您重定向到默认区域的HomeControllerIndex,但是当我注销时,我留在Admin 区域。如何重定向回管理区域?

在导航中登录

     <ul class="nav masthead-nav">
          <li class="active"><a href="@Url.Action("Index", "Home")">Domů</a></li>
          <li><a href="@Url.Action("About", "Home")">O nás</a></li>
          <li><a href="@Url.Action("Index", "Login", new {area = "Admin" })">Vypis lyží/snowboardů</a></li>
     </ul>

登录控制器操作注册/注销

public ActionResult SignIn(string login, string password)
        {
            if (Membership.ValidateUser(login, password))
            {
                FormsAuthentication.SetAuthCookie(login, false);
                return RedirectToAction("Index", "Home");
            }

            TempData["error"] = "Login nebo heslo není správné";
            return RedirectToAction("Index", "Login");
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            Session.Clear();

            return RedirectToAction("Index", "Home");
        }

路由配置

 public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new [] { "pujcovna_lyze.Controllers" }
            );
        }
    }

【问题讨论】:

  • 嘿,祝你的项目好运!您有什么具体问题要问我们还是...?
  • @GrumpyCrouton 你是什么意思?我认为从文字中可以清楚地看出,但是我添加了我的问题。

标签: c# asp.net asp.net-mvc visual-studio asp.net-mvc-areas


【解决方案1】:

我通过在Logout() 操作中添加new { area = "" } 参数解决了这个问题。

动作现在看起来像这样:

public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            Session.Clear();

            return RedirectToAction("Index", "Home", new { area = "" });
        }

【讨论】:

    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2011-06-19
    • 2019-06-09
    • 2013-06-02
    • 2011-06-18
    • 1970-01-01
    相关资源
    最近更新 更多