0

I had a problem and it's been solved in the bellowing link:

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

But I have another question. When I added another route like this:

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

The previous route doesn't work. I mean it again gets null value. Whenever I want to call its method I bring its route to the top of the other routes manually! And it gets work! You know I have to change each route's priority when I want to call its method. O.O Any Idea? Thanks In advance.

Thi is the total rout.config in my project.

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}",
       defaults: new { controller = "Authentication", action = "VerifyAccount" }
   );

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

And these are ActionMethods:

 [HttpGet]
    public ActionResult ResetPassword(string passwordResetCode)
    {
        ResetPasswordViewModel resetPasswordViewModel = new ResetPasswordViewModel();
        ResponseMessage<User> passwordResetRequestedUser = _userService.ResetPasswordCode(passwordResetCode);
        if (passwordResetRequestedUser.IsSuccess )
            resetPasswordViewModel.ResetCode = passwordResetCode;
            return View(resetPasswordViewModel);
    }

 [HttpGet]
    public ActionResult VerifyAccount(string activationCode)
    {
        if (activationCode != null)
        {
            ResponseMessage<User> verifiedUser = _userService.VerifyAccount(activationCode);
            ModelState.AddModelError("AccountVerification", verifiedUser.ErrorMessages[0]);
            return View(verifiedUser.Result.ConvertToUserViewModel());
        }
        return new HttpNotFoundResult();
    }
  • What other route? (you need to include it it your question). And the route you have shown also matches the default route (and why did you accept a wrong answer in the other question?) –  Apr 30 '18 at 07:30
  • hi @StephenMuecke. Thanks for your attention. That wasn't wrong. it worked correctly. After I got result from previous question I needed to add on other route. Sure. I'm going to edit my question. – Atefeh Mohammadpoor Apr 30 '18 at 07:35
  • It is wrong! (see my answer on your other question). And your question need to include code, not images of it –  Apr 30 '18 at 07:42
  • Ok @StephenMuecke I 'm going to do what you suggested. – Atefeh Mohammadpoor Apr 30 '18 at 07:43

1 Answers1

1

I suggest you to specify exactly the controller and action that you are going to use on customized routes as to not override the default route.

Ex.

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

routes.MapRoute(
   name: "PasswordReset",
   url: "Authentication/ResetPassword/{passwordResetCode}",
   defaults: new { controller = "Authentication", action = "ResetPassword", passwordResetCode = = UrlParameter.Optional }
   );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • Would you please tell me how to do it correctly? – Atefeh Mohammadpoor Apr 30 '18 at 07:49
  • Look at my answer and you can put it on your route.config file. First 2 routes are specifically for your verify and reset password urls. You must specify the controller and action on the url. Your routes right now is always taking the first registered route because the url you set accepts any controller and action that follows the standard controller/action/parameter format but since you have specified a different parameter name, urls that are expecting a parameter and don't have that parameter name specified becomes null. – Luke Villanueva Apr 30 '18 at 08:40
  • Got it. Thank you :) – Atefeh Mohammadpoor Apr 30 '18 at 09:36