ASP.NET - Get website's URL without HttpContex

2019-07-15 23:38发布

Bit of a long shot, but is there a way in ASP.NET to dynamically get the website's URL (http://www.example.com) when there is no HttpContext.Current available.

There is no HttpContext because the code is running in a background thread* (but under the ASP.NET AppDomain). I have a background process that sends emails out every evening, and needs to include the Web address, but I don't want to hard code it because of deployments and testing (it changes from http://localhost:12345 to http://testing.example.com and then to http://www.example.com for the live site).

*Please don't advise a Windows Service, I'm aware of these but hosting restrictions prevent me from doing this.

2条回答
Lonely孤独者°
2楼-- · 2019-07-16 00:27

Judging by your weird hosting restrictions (seriously, just buy a decent server instead, saves you a lot of trouble) you're starting the thread from an ASP.NET page. Just feed the URL in when you're starting the thread and store it in a field.

查看更多
相关推荐>>
3楼-- · 2019-07-16 00:32

Your app must construct and start the thread. At the point the thread is created, the HttpContext.Current should be available, store the relevant part of the web address that is important and either pass to the constructor of your object that wraps the threading functionality, or if you dont have a custom object, store it in a location where the thread can access it. You will need to enter a critcial section with lock() {} if you use the latter approach.

查看更多
登录 后发表回答