7

I have this string to format and the exception is thrown on 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));
}

confirmationToken and email are 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 the 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()
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Zinov
  • 3,817
  • 5
  • 36
  • 70
  • And what is the exception being thrown? – Peter M May 28 '15 at 18:39
  • @PeterM Value does not fall within the expected range – Zinov May 28 '15 at 18:40
  • And what happens when you substitute the parameters for string literals? – Peter M May 28 '15 at 18:42
  • @JasonSlocomb He is assigning 2 arguments. Look closely at the assignement – Dudemanword May 28 '15 at 18:46
  • I just proposed and edit, hopefully it will help understanding the used parameters – Luiso May 28 '15 at 18:48
  • I believe the issue you are having is within the `Url.Action()` method. Have tried it without the last parameter? – Luiso May 28 '15 at 18:49
  • @Luiso yes I tried and nothing, still the same error – Zinov May 28 '15 at 18:52
  • @David Passmore .. editing the question that way is not in the spirit of SO. See this meta answer http://meta.stackoverflow.com/a/260246/31326 – Peter M May 28 '15 at 18:54
  • @CBauer the error is related to GenerateUrl in MVC, but I don't know what is wrong on this – Zinov May 28 '15 at 18:55
  • @PeterM, Apologies... – David Passmore May 28 '15 at 18:56
  • @Zinov ***Nothing wrong with string body = string.Format(...);. I tested it, and it does not throw any exception.*** – Win May 28 '15 at 18:58
  • have you seen the generated url, can you say anything more specific about the exception you are getting? – Luiso May 28 '15 at 19:01
  • @Zinov Please post a stack trace, take a look here: http://stackoverflow.com/questions/4272579/how-to-print-full-stack-trace – C Bauer May 28 '15 at 19:01
  • @CBauer I put the StackTrace, as you can see GenerateUrl method is throwing an exception – Zinov May 28 '15 at 19:08
  • 4
    @Zinov So from the stack trace, the error is happening in `Url.Action`, so the `string.Format()` is irrelevant here and should not be included in the question. You need to troubleshoot and figure out there the error is coming from: try removing the 4th or 3rd and 4th parameters from `Url.Action()`. Try removing `confirmationToken` or `email` or both. Figure out what is the minimal amount of code to reproduce the error, and then we can help you. – JLRishe May 28 '15 at 19:10
  • @bzlm yes is SendEmailConfirmation – Zinov May 28 '15 at 19:12
  • @Zinov, have you tried all of [JLRishe's suggestions](http://stackoverflow.com/questions/30514551/exception-value-does-not-fall-within-the-expected-range-in-asp-net-mvc-contro#comment49105382_30514551) already? If not, go do that instead of hanging out here. :) – bzlm May 28 '15 at 19:14
  • Possibly related: http://stackoverflow.com/questions/16202921/error-message-from-dotnetopenauths-readauthorizationrequest-when-passing-in-htt When is the `SendEmailConfirmation()` method called? Is a controller action calling it? Is it a controller action itself? Something else? – JLRishe May 28 '15 at 19:15
  • @bzlm the minimum amount is Url.Action("ConfirmEmail"), even that one is throwing the exception – Zinov May 28 '15 at 19:18
  • @JLRishe SendEmailConfirmation is called from another controller – Zinov May 28 '15 at 19:18
  • @Zinov Can you show us the method from which `SendEmailConfirmation()` is called? – JLRishe May 28 '15 at 19:29
  • Did you ever figure this out? – Mike Flynn Apr 18 '22 at 12:40
  • @MikeFlynn check the accepted answer below – Zinov Apr 18 '22 at 22:54

1 Answers1

15

The URL helper might be trying to create a URL after the request has already been destroyed (because it's async). In that case you will get an error when trying Url.Action().

Try generating the body of the email in a non-async controller and send it into your email class as an argument.

The question Custom task runner method throws ArgumentException seems to be closely related to what you're seeing (down to the stacktrace and the method the OP arrived at an answer).

Community
  • 1
  • 1
C Bauer
  • 5,003
  • 4
  • 33
  • 62