I have a static class that sends emails with links to certain pages of my site. That link is getting dynamically generated with this code:
UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
string url = urlHelper.Action("Details", "Product", new { id = ticketId }, "http");
The problem is I now also have a service that periodically compares the creation date with the current date and sends those mails automatically. The code crashes of course and says HttpContext.Current
is null (because it ain't a request).
I tried some things like that:
private static System.Web.Routing.RequestContext requestContext;
private static System.Web.Routing.RequestContext RequestContext {
get
{
if(requestContext == null)
requestContext = HttpContext.Current.Request.RequestContext;
return requestContext;
}
}
But when I need RequestContext the second time for the UrlHelper.Action crashes saying Null Reference Exception.
I failed to somehow save/remember/pass the UrlHelper or the HttpContext to have access when calling the mail method over my service.