【问题标题】:C# MVC 5 routingC# MVC 5 路由
【发布时间】:2018-07-31 08:07:23
【问题描述】:

我希望你能帮助我:) 我正在尝试在我的 .net mvc 应用程序中执行注销功能,但我制作的链接永远不会到达正确的路线。这是我的代码:

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 }
            );
        }

public ActionResult LogOff()
    {
        Session[UserSessionIdentifier] = null;
        return RedirectToAction("Index", "Home");
    }

    @Html.ActionLink("Log off", "LogOff", "Account")

当我点击这个链接时,我的浏览器中有这个网址:

http://localhost:52041/Account/Login?ReturnUrl=%2FAccount%2FLogOff

我的错误在哪里?

【问题讨论】:

  • 您是否在控制器中激活了AuthorizeAttribute?听起来你应该在销毁注销用户的会话数据后用[AllowAnonymous] 标记LogOff 操作。
  • 好的,我的班级有一个 [Authorize] 属性,这就是为什么!在 LogOff 方法上允许 [AllowAnonymous] 有效,谢谢 :)

标签: c# model-view-controller routing


【解决方案1】:

在您的注销功能中,您应该清除所有会话变量。 而不是 @HTML.ActionLink() 尝试使用 注销

【讨论】:

    【解决方案2】:

    我不确定您是否看到了,但我的问题已解决,答案在第一篇 cmets 中。 无论如何,感谢有关清除会话变量的提示:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-01
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2015-04-06
      相关资源
      最近更新 更多