tags:

views:

7

answers:

2

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 genererating a URL for the client when the HttpContext is not available.

A: 

There is always an HttpContext. Try the HttpContext.Current static method.

jfar
A: 

You can capture the url in the Application_Start method and store it in the ApplicationState collection.

Sruly
Yep, works perfectly. protected void Application_Start() { _siteUri = HttpContext.Current.Request.Url; }
Pete Nelson
You should be able to access without the HttpContext.Current part. The request object is directly accessible within Application_Start
Sruly