【问题标题】:The activationCode is null in my method input when I click the email Verification link当我单击电子邮件验证链接时,我的方法输入中的 activationCode 为空
【发布时间】:2018-10-08 02:49:05
【问题描述】:

以上帝的名义

大家好。我正在 VS 2017 中为我的 mvc 5 网站创建注册。它有电子邮件确认。激活的 URL 链接将在电子邮件中完整接收。当我单击时,它可以正常工作,并且它恰好在正确的 ActionMethod 上到达我的控制器,但我不知道为什么activationCode 为空! :| 在它正常工作之前,我的意思是激活码不为空。我不知道它发生了什么!

任何帮助将不胜感激。

已编辑:

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

        routes.MapRoute(
       name: "Password",
       url: "{controller}/{action}/{passwordResetCode}",
       defaults: new { controller = "Authentication", action = "ResetPassword" }
   );
        routes.MapRoute(
       name: "Activation",
       //url: "{controller}/{action}/{activationCode}",
       url: "Authentication/VerifyAccount/{activationCode}", 
       defaults: new { controller = "Authentication", action = "VerifyAccount" }
   );

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

【问题讨论】:

  • 请在接收此 URL 并显示代码页的操作旁边提供发送到用户电子邮件的 URL 示例
  • var verifyURL = "/Authentication/VerifyTheAccount/" + activationCode; var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, verifyURL);示例:localhost:0000/Authentication/VerifyTheAccount/…
  • 并且在控制器内部,您应该接受名称 id 和类型 id GUID 的参数,除此之外检查您的路由是否接受 {id} 参数
  • @MahmoudHeretani 感谢您关注我的问题。我编辑了问题,并在其中放入了另一张图片。请你看看好吗?
  • @MahmoudHeretani 你能给我一个例子吗?我想我必须把路线放在行动上。但不知道写什么正确。

标签: asp.net-mvc-5 registration email-verification


【解决方案1】:

您的默认路由接受名为id 的参数,而不是activationCode。您要么需要将控制器方法更改为

public ActionResult VerifyTheAccount(string id)

并更改链接以设置id,例如(来自您的 cmets)

var verifyURL = "/Authentication/VerifyTheAccount/" + activationCode

或使用首选的Url.Action() 方法

var verifyURL = '@Url.Action("VerifyTheAccount", "Authentication", new { id = activationCode })

或者,您需要在 DefaultRoute 之前创建一个特定的路由定义

routes.MapRoute(
   name: "Activation",
   url: "Authentication/VerifyTheAccount/{activationCode}",
   defaults: new { controller = "Authentication", action = "VerifyTheAccount" }
);

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

【讨论】:

  • 我也这样做了。但我没有得到答案。
  • 对不起,我不明白你的评论。你是什​​么意思但我没有得到答案。
  • Y.W.C 我没有得到任何答案。你懂的?现在我明白了,最好在其上指定每个 actionMerhod 的路由,并且不要更改默认的 Rout.config。但我不知道在我的 actionMethods 之上究竟要写什么。你知道吗?
  • 完全按照我的回答-您在默认路由之前创建了一条特定路由-在您的情况下它具有url: "Authentication/VerifyTheAccount/{activationCode}",
  • 在 Action 方法之上?喜欢 VerifyTheAcount?
【解决方案2】:

为了实现您的需要,您必须将操作参数名称更改为 id 或者您可以向 RouteConfig 添加额外的路由,如下所示:

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

        routes.MapRoute(
           name: "Activation",
           url: "{controller}/{action}/{activationCode}",
           defaults: new { controller = "Authentication", action = "VerifyTheAccount" }
       );

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

    }

注意路由定义的顺序很重要。

现在运行应用程序时,您将获得以下结果:

在浏览器内部:

【讨论】:

  • 哇...谢谢一百万。你真的拯救了我的一天。希望能实现你的任何美好愿望。
  • @MahmoudHeretani 再次嗨。您的解决方案工作正常。但是当我添加另一个路由时,例如“ routes.MapRoute( name: "Password", url: "{controller}/{action}/{passwordResetCode}", defaults: new { controller = "Authentication", action = "ResetPassword" } );"之前的路线不行。我的意思是它再次获得空值。每当我想调用它的方法时,我都会手动将它的路由带到其他路由的顶部!它得到了工作!你知道当我想调用它的方法时,我必须改变每条路由的优先级。 O.O 有什么想法吗?提前致谢...
  • 错了!两条路线是相同的 - 两者都接受 0 和 3 段之间(Default 路线永远不会被击中)
  • 请删除所有路由并保留默认路由,除此之外,将所有参数名称从 passwordResetCode 和 activationCode 更改为 Id 这应该可以解决问题
猜你喜欢
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 2011-05-25
  • 1970-01-01
相关资源
最近更新 更多