改变每条路线的优先级只是得到他们的行动方法工作
Change each route's priority just gets their actionMethod work
我有一个问题,它已经在下面解决了 link:
但我还有一个问题。
当我添加这样的另一条路线时:
routes.MapRoute( name: "Password", url: "{controller}/{action}/{passwordResetCode}", defaults: new { controller = "Authentication", action = "ResetPassword" } );
之前的路线不行。我的意思是它再次获得空值。每当我想调用它的方法时,我都会手动将它的路由带到其他路由的顶部!它开始工作了!你知道当我想调用它的方法时我必须改变每条路线的优先级。 O.O 有什么想法吗?提前致谢。
这是我项目中的总数 rout.config。
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 }
);
}
}
这些是 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();
}
我建议您准确指定要在自定义路由上使用的控制器和操作,以免覆盖默认路由。
例如。
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 }
);
我有一个问题,它已经在下面解决了 link:
但我还有一个问题。 当我添加这样的另一条路线时:
routes.MapRoute( name: "Password", url: "{controller}/{action}/{passwordResetCode}", defaults: new { controller = "Authentication", action = "ResetPassword" } );
之前的路线不行。我的意思是它再次获得空值。每当我想调用它的方法时,我都会手动将它的路由带到其他路由的顶部!它开始工作了!你知道当我想调用它的方法时我必须改变每条路线的优先级。 O.O 有什么想法吗?提前致谢。
这是我项目中的总数 rout.config。
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 }
);
}
}
这些是 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();
}
我建议您准确指定要在自定义路由上使用的控制器和操作,以免覆盖默认路由。
例如。
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 }
);