How to resolve application URL from background thread in ASP.NET MVC?

The application splits into two streams; The main web application and the secondary thread used to handle asynchronous events. The secondary stream receives an event in which it needs to send an email with the full URL of the main application (plus additional route arguments) inside it.

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

Of course, in the background thread there is no way to use HttpContext.Current to resolve Url, because there is no request. No HttpRequest, No HttpContext ...

I found that most ASP.NET methods (even with MVC) used to create URLs are based on HttpContext. Is there a way to create a fully qualified application URL in ASP.NET without using HttpContext or any of its derivatives?

I am looking for a safe thread, for example:

UrlHelper.GetApplicationtUrl() 

Any ideas? Your suggestions are greatly appreciated.

+6
source share
2 answers

I had this exact problem. I ended up saving the URL in the web.config file. I did it like this:

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

and called it as it happens at the service level:

 string confirmUrl = string.Format(ConfigurationManager.AppSettings["ConfirmUrl"], confirmCode); 
+1
source

If you cannot just use the configuration file when creating Thread, use the ThreadStart delegate to provide the basic information needed for the new thread.

0
source

Source: https://habr.com/ru/post/894683/


All Articles