C#MVC:用户密码重置控制器:电子邮件地址作为用户名的问题
我编写了下面的代码,用于在C#MVC应用程序中重置用户密码(我使用的是aspnet成员api),并在示例教程应用程序(MVC Music Store)上成功进行了测试。如果您想先阅读问题描述,请跳到结尾 非活动用户视图(部分视图) 更改用户密码获取和发布控制器C#MVC:用户密码重置控制器:电子邮件地址作为用户名的问题,c#,asp.net-mvc,routelink,C#,Asp.net Mvc,Routelink,我编写了下面的代码,用于在C#MVC应用程序中重置用户密码(我使用的是aspnet成员api),并在示例教程应用程序(MVC Music Store)上成功进行了测试。如果您想先阅读问题描述,请跳到结尾 非活动用户视图(部分视图) 更改用户密码获取和发布控制器 public ActionResult changeUserPassword(string username) { ViewData["username"] = username;
public ActionResult changeUserPassword(string username)
{
ViewData["username"] = username;
return View();
}
[HttpPost]
public ActionResult changeUserPassword(ChangePasswordModel model, FormCollection values)
{
string username = values["username"];
string password = values["password"];
string confirmPassword = values["confirmPassword"];
MembershipUser mu = Membership.GetUser(username);
if (password == confirmPassword)
{
if (mu.ChangePassword(mu.ResetPassword(), password))
{
return RedirectToAction("Index", "ControlPanel");
}
else
{
ModelState.AddModelError("", "The current password does not meet requirements");
}
}
return View();
}
我还修改了Global.asax.cs文件,以适应我在InactiveUsers部分中的路线:
// Added in 10/01/11
RouteTable.Routes.MapRoute(
"AdminPassword", // routename
"ControlPanel/changeUserPassword/{username}",
new { controller = "ControlPanel", action = "changeUserPassword", username = UrlParameter.Optional }
);
// END
现在,当我在MVC音乐商店上测试时,我所有的用户名都是单词,例如管理员、用户等。但是现在我正在将此代码应用到我工作场所的某个情况中,结果并没有按计划进行。我工作场所使用的用户名实际上是电子邮件地址,我认为这就是问题的根源
当我在partial InactiveUsers视图中单击RouteLink时,它会将我带到重置密码页面,其url如下所示:
但是,
当我点击RouteLink时,会发生一个错误,提示找不到view changeUserPassword,URL如下所示:
-看看“@”符号是怎么搞砸的
我还通过代码进行了调试,在我的GET-changeUserPassword中,用户名填充正确:example1@gmail.com,所以我想只是URL把事情搞砸了
如果我手动输入URL,则会显示changeUserPassword视图,但是密码重置功能不起作用。在if(mu.ChangePassword(mu.ResetPassword(),password))行抛出“对象引用未设置为对象实例”异常
我想如果我能解决第一个问题(URL'@'符号问题),它可能会帮助我解决第二个问题
任何帮助都将不胜感激:)
堆栈跟踪-按要求执行
源错误:
在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别
堆栈跟踪:
[InvalidOperationException: The view 'changeUserPassword' or its master was not found. The following locations were searched:
~/Views/ControlPanel/changeUserPassword.aspx
~/Views/ControlPanel/changeUserPassword.ascx
~/Views/Shared/changeUserPassword.aspx
~/Views/Shared/changeUserPassword.ascx]
System.Web.Mvc.ViewResult.FindView(ControllerContext context) +495
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +60
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +391
System.Web.Mvc.<>c__DisplayClass16.<InvokeActionResultWithFilters>b__13() +61
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +285
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830
System.Web.Mvc.Controller.ExecuteCore() +136
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +65
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
[InvalidOperationException:未找到视图“changeUserPassword”或其主视图。搜索了以下位置:
~/Views/ControlPanel/changeUserPassword.aspx
~/Views/ControlPanel/changeUserPassword.ascx
~/Views/Shared/changeUserPassword.aspx
~/Views/Shared/changeUserPassword.ascx]
System.Web.Mvc.ViewResult.FindView(ControllerContext上下文)+495
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文)+208
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext ControllerContext,ActionResult ActionResult)+39
System.Web.Mvc.c__DisplayClass14.b__11()+60
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器,ResultExecutingContext预文本,Func`1 continuation)+391
System.Web.Mvc.c__显示类16.b__13()+61
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext ControllerContext,IList`1过滤器,ActionResult ActionResult)+285
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+830
System.Web.Mvc.Controller.ExecuteCore()+136
System.Web.Mvc.ControllerBase.Execute(RequestContext-RequestContext)+111
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext RequestContext)+39
System.Web.Mvc.c__显示类8.b__4()+65
System.Web.Mvc.Async.c__显示类1.b__0()+44
System.Web.Mvc.Async.c_uuudisplayClass8`1.b_uuu7(IAsyncResult)+42
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+141
System.Web.Mvc.Async.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+54
System.Web.Mvc.Async.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+52
System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest(IAsyncResult结果)+38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+8841105
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+184
您是否意识到%40是@符号的URL编码版本?你的URL中的数据不是乱七八糟的,而是经过URL编码的。这是必需的,因为您的数据包含一个在URL中有意义的符号,所以必须以不破坏URL的方式对其进行编码
您可以在此处尝试URL编码/解码:
您是否意识到%40是@符号的URL编码版本?你的URL中的数据不是乱七八糟的,而是经过URL编码的。这是必需的,因为您的数据包含一个在URL中有意义的符号,所以必须以不破坏URL的方式对其进行编码 您可以在此处尝试URL编码/解码:
最好使用
System.Web.HttpContext.Current.Server.UrlDecode(url1)
。
无论Url是否编码,它都会返回一个原始Url,如“http://localhost:83/ControlPanel/changeUserPassword/example1@“gmail.com”,您可以从中获取用户名
string userName = System.Web.HttpContext.Current.Server.UrlDecode(userName);
RouteTable.Routes.MapRoute(
"AdminPassword", // routename
"ControlPanel/changeUserPassword/" + userName,
new { controller = "ControlPanel", action = "changeUserPassword", username = UrlParameter.Optional }
);
最好使用
System.Web.HttpContext.Current.Server.UrlDecode(url1)
。
无论Url是否编码,它都会返回一个原始Url,如“http://localhost:83/ControlPanel/changeUserPassword/example1@“gmail.com”,您可以从中获取用户名
string userName = System.Web.HttpContext.Current.Server.UrlDecode(userName);
RouteTable.Routes.MapRoute(
"AdminPassword", // routename
"ControlPanel/changeUserPassword/" + userName,
new { controller = "ControlPanel", action = "changeUserPassword", username = UrlParameter.Optional }
);
当找不到视图时,异常通常会告诉您搜索的位置。您是否验证了
~/Views/ControlPanel/changeurspassword.aspx
是否存在?嗨,达林,是的,它确实存在。如果我手动输入URL,视图就会显示出来。当我点击RouteLink时,我得到了这个错误消息。你能发布准确的错误消息stack trace吗?嗨,Darin,stack trace发布了。谢谢,这很奇怪。异常表示该文件不存在
string userName = System.Web.HttpContext.Current.Server.UrlDecode(userName);
RouteTable.Routes.MapRoute(
"AdminPassword", // routename
"ControlPanel/changeUserPassword/" + userName,
new { controller = "ControlPanel", action = "changeUserPassword", username = UrlParameter.Optional }
);