Search code examples
asp.net-mvc-5registrationemail-verification

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


In the name of God

Hi all. I'm creating registration for my mvc 5 website in VS 2017.It has Email confirmation in it. the URL link for activation will be received completely in Email. when I click , it works and it exactly comes to my controller on the correct ActionMethod but I don't know why the activationCode is null! :| While before it worked correctly, I mean the Activation code was not null. I don't know what happend to it! Image enter image description here

Any help will be appreciated.

Edited:

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

Solution

  • Your default route accepts a parameter named id, not activationCode. You either need to change the controller method to

    public ActionResult VerifyTheAccount(string id)
    

    and change the link to set the id, for example (from your comments)

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

    or using the preferred Url.Action() method

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

    Alternatively you need to create a specific route definition before the 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 }
    );