Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Resolve an application URL from a background thread in ASP.NET MVC?

The app splits off into two threads; the main web app and a secondary thread used for asynchronous event handling. The secondary thread receives an event where it needs to send an email with a fully qualified URL of the main app (plus additional route arguments) inside it.

Eg. http://Server.com/App/RouteData?AdditionalArguments

Of course the background thread does not have the luxury of using HttpContext.Current to resolve a Url, because there's no request. No HttpRequest, no HttpContext...

I discovered that most of the methods ASP.NET (even with MVC) uses to build URLs rely on HttpContext. Does there exist a way to build a fully qualified application URL in ASP.NET without using HttpContext or any of it's derivatives?

I'm looking for a thread-safe method like:

UrlHelper.GetApplicationtUrl()

Any Ideas? Your suggestions are much appreciated.

like image 420
Nautic20 Avatar asked Aug 09 '11 22:08

Nautic20


1 Answers

I had this exact problem. I ended up storing the url in the web.config file. I did mine like so:

<appSettings>
  <!-- Urls -->
  <add key="AppDomain" value="http://localhost:1273/" />
  <add key="ConfirmUrl" value="http://localhost:1273/Auth/Confirm/?code={0}" />
</appSettings>

and called it like this in the service layer:

string confirmUrl = string.Format(ConfigurationManager.AppSettings["ConfirmUrl"], confirmCode);
like image 121
Shawn Mclean Avatar answered Nov 15 '22 06:11

Shawn Mclean