当我点击邮件验证时,我的方法输入中的 activationCode 为 null link

The activationCode is null in my method input when I click the email Verification link

以上帝的名义

大家好。我正在 VS 2017.It 中为我的 mvc 5 网站创建注册,其中包含电子邮件确认。用于激活的URL link 将在电子邮件中完整接收。当我单击时,它可以正常工作,并且它完全按照正确的 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 }
        );
    }
}

为了实现您的需要,您必须将操作参数名称更改为 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 }
        );

    }

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

现在当运行申请时你会得到如下结果:

在浏览器中:

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

public ActionResult VerifyTheAccount(string id)

并更改 link 以设置 id,例如(来自您的评论)

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