How to send callback link with Url.Action in custom class at ASP.NET MVC

1.1k views Asked by At

I am working on a survey application in Asp.Net Mvc. I select the customer list for related survey from my page and send a link with GuidId to these people from controller. The customer receiving the mail will be directed to the survey page by clicking the button in the mail. However, since sending mail can be multiple, I submit the mail to a background task after saving the guid to the database. The background flag consists of several methods that call each other in a custom class. To create a callbackUrl link, I pass UrlHelper from the controller to the cnstructor of the cutom class. I create the callback url in the custom class as follows.

Where am I making a mistake? What is the cause of this error? How can I solve this problem?

I am calling background task in custom class as follows

new EmailOperation(new UrlHelper(this.ControllerContext.RequestContext)).SendAsync(bodyHtml, surveyId);

public class EmailOperation
{
    private UrlHelper _urlHelper;
    public EmailOperation(UrlHelper urlHelper)
    {
        _urlHelper = urlHelper;
    }

    public Task SendAsync(string bodyHtml, int? surveyId)
    {
        return Task.Factory.StartNew(() => { GetData(bodyHtml, surveyId); });
    }
}


 void GetData(string bodyHtml, int? surveyId)
{
  ...
  SendEmail(bodyHtml, surveyCustomers);
}




private void SendEmail(string bodyHtml, List<SurveyCustomers> surveyCustomers)
{
     foreach (SurveyCustomers surveyCustomer in surveyCustomers)
     {
         //_urlHelper is passed by my controller to this custom class
         string scheme = _urlHelper.RequestContext.HttpContext.Request.Url.Scheme;
         string callbackUrl = _urlHelper.Action("SubmitSurvey", "Survey", new { token }, scheme);//Error on this line


         SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
          ...
         smtpClient.Send(msg);
     }
}

But I am getting an error as follows;

An exception of type 'System.ArgumentException' occurred in System.Web.dll but was not handled in user code

Additional information: The value is not in the expected range.

0

There are 0 answers