Exception: "The value is not in the expected range" in the ASP.NET MVC controller

I have this line for formatting and an exception is thrown in this part (string body ....)

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

confirmationTokenand emailare strings, and my ConfirmEmail is

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

and this is 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()
+4
source share
1 answer

The URL helper may try to create a URL after the request has already been destroyed (because it is asynchronous). In this case, you will receive an error message when you try Url.Action ().

Try creating an email body in a non-asynchronous controller and sending it to your email class as an argument.

ArgumentException, , , ( stacktrace , OP ).

+9

All Articles