如何从ASP.NET MVC中的后台线程解析应用程序URL?

Nau*_*c20 6 asp.net asp.net-mvc httpcontext thread-safety

该应用程序分为两个线程; 主Web应用程序和用于异步事件处理的辅助线程.辅助线程接收一个事件,它需要发送一封电子邮件,其中包含主应用程序的完全限定URL(以及其他路由参数).

例如.http://Server.com/App/RouteData?AdditionalArguments

当然后台线程没有使用HttpContext.Current来解析Url的奢侈,因为没有请求.没有HttpRequest,没有HttpContext ......

我发现ASP.NET(甚至是MVC)用于构建URL的大多数方法都依赖于HttpContext.是否存在在ASP.NET中构建完全限定的应用程序URL而不使用HttpContext或其任何衍生物的方法?

我正在寻找一个线程安全的方法,如:

UrlHelper.GetApplicationtUrl()
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?非常感谢您的建议.

Sha*_*ean 2

我遇到了这个问题。我最终将 url 存储在 web.config 文件中。我是这样做的:

<appSettings>
  <!-- Urls -->
  <add key="AppDomain" value="http://localhost:1273/" />
  <add key="ConfirmUrl" value="http://localhost:1273/Auth/Confirm/?code={0}" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

并在服务层这样调用它:

string confirmUrl = string.Format(ConfigurationManager.AppSettings["ConfirmUrl"], confirmCode);
Run Code Online (Sandbox Code Playgroud)