Generating a client URL in a Timer running in global.asax
I have an MVC 2 app that has a System.Timers.Timer object starting up during Application_Start in global.asax. One of the things this timer needs to do is generate a daily e-mail to users when they have an item to review in their work queue. Inside the e-mail is a link back to the item to review.
In code I have running in various controller actions, I use code like this to generate URLs that can be e-mailed to users:
UriBuilder builder = new UriBuilder(Request.Url);
builder.Path = Url.RouteUrl("ReviewDetails", new { id = reviewId });
return builder.ToString();
I would like to do the same inside my Timer's elapsed method in global.asax, but there is no HttpContext at that point, so I'm not able to programatically determine the URL.
I think the only solution is to store the site's root (ex: http://intranet.domain.com/MyApp) in the web.config and then use that to build the URL, like this:
string url = string.Concat(ConfigurationManager.AppSettings["SiteRoot"], "/Review/", reviewId.ToString());
I'm putting this out in the cloud to see if there are any better solutions to programatically genererat开发者_开发问答ing a URL for the client when the HttpContext is not available.
You can capture the url in the Application_Start method and store it in the ApplicationState collection.
There is always an HttpContext. Try the HttpContext.Current
static method.
精彩评论