例外:“值不在预期范围内”.在ASP..NET MVC控制器
本文关键字:ASP NET 控制器 MVC 范围内 例外 | 更新日期: 2023-09-27 17:51:05
我要格式化这个字符串,在这部分(字符串主体....)抛出异常
private Task SendEmailConfirmation(UserModel user)
{
var emailService = new EmailUnsecServiceAgent(null);
string body = string.Format("Dear {0} <BR/>Thank you for your registration, " +
"please click on the below link to complete your" +
" registration: <a href='"{1}'" title='"User Email Confirm'">{1}</a>",
user.UserName,
Url.Action("ConfirmEmail",
"Account",
new
{
confirmationToken = user.ConfirmationToken,
email = user.Email
},,
Request.Url.Scheme));
return Task.Run(() => emailService.SendEmail("Confirm your account", body, null, true, null, null, null));
}
confirmationToken
和email
是字符串,我的ConfirmEmail是
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string confirmationToken, string email)
{
var securityService = new SecurityUnsecServiceAgent(null);
UserModel user = securityService.GetUserByConfirmationToken(confirmationToken);
if (user != null)
{
if (user.Email == email)
{
user.ConfirmedEmail = true;
securityService.UpdateUser(user);
return View("RegisterMessage");
}
return View("ConfirmEmail", user);
}
return RedirectToAction("ConfirmEmail", user);
}
这是StackTrace:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)
at System.Web.WebPages.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext)
at System.Web.WebPages.UrlRewriterHelper.WasRequestRewritten(HttpContextBase httpContext)
at System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath)
at System.Web.WebPages.UrlUtil.GenerateClientUrl(HttpContextBase httpContext, String contentPath)
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues)
at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues)
at SendEmailConfirmation(UserModel user) in c:'Projects'..'Controllers'AccountController.cs:line 361
at Controllers.AccountController.<>c__DisplayClass21.<Register>b__1d() in c:'Projects'..'Controllers'AccountController.cs:line 318
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
URL帮助器可能在请求已经被销毁(因为它是异步的)之后尝试创建一个URL。在这种情况下,当您尝试Url.Action()时将得到一个错误。
尝试在非异步控制器中生成电子邮件的正文,并将其作为参数发送到email类中。
问题自定义任务运行器方法抛出ArgumentException似乎与您所看到的密切相关(向下到堆栈跟踪和OP到达答案的方法)。