The activationCode is null in my method input when

2019-02-21 02:53发布

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

2条回答
Viruses.
2楼-- · 2019-02-21 03:41

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: Debug result

And inside the browser: enter image description here

查看更多
劫难
3楼-- · 2019-02-21 03:57

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 }
);
查看更多
登录 后发表回答