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

时间:2022-01-21 04:12:07

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.

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

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...

当然后台线程没有使用HttpContext.Current来解析Url的奢侈,因为没有请求。没有HttpRequest,没有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?

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

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

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

UrlHelper.GetApplicationtUrl()

Any Ideas? Your suggestions are much appreciated.

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

2 个解决方案

#1


2  

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

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

<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);

#2


0  

If you can't just use the configuration file, when creating the Thread, use the ThreadStart delegate to provide the base information you need to the new thread.

如果您不能只使用配置文件,那么在创建Thread时,请使用ThreadStart委托向新线程提供所需的基本信息。

#1


2  

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

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

<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);

#2


0  

If you can't just use the configuration file, when creating the Thread, use the ThreadStart delegate to provide the base information you need to the new thread.

如果您不能只使用配置文件,那么在创建Thread时,请使用ThreadStart委托向新线程提供所需的基本信息。