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

一曲冷凌霜 提交于 2019-12-01 13:32:07

问题


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!

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

回答1:


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



回答2:


In order to achieve what you need you have to change the action parameter name to id or you can add extra route to your RouteConfig as following:

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

    }

Note that the order of routes definition is very important.

Now when running the application you will get the following results:

And inside the browser:



来源:https://stackoverflow.com/questions/50059321/the-activationcode-is-null-in-my-method-input-when-i-click-the-email-verificatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!