ASP.NET MVC:如何创建可用的 UrlHelper 实例?
我正在使用quartz.net 来安排asp.net mvc 应用程序中的定期事件。
计划作业应调用需要 UrlHelper 实例的服务层脚本(用于根据服务将发送的电子邮件中包含的正确路由(via urlHelper.Action(..)
)创建 URL) 。
我不想将链接硬编码到电子邮件中 - 应使用 urlhelper 来解析它们。
作业:
public class EvaluateRequestsJob : Quartz.IJob
{
public void Execute(JobExecutionContext context)
{
// where to get a usable urlHelper instance?
ServiceFactory.GetRequestService(urlHelper).RunEvaluation();
}
}
请注意,它不在 MVC 管道内运行。当前没有正在服务的请求,代码由 Quartz 调度程序在定义的时间运行。
如何获取可在指定位置使用的 UrlHelper 实例?
如果无法构造 UrlHelper,我看到的另一个选项是通过执行 HTTP 请求使作业“自调用”控制器操作 - 在执行操作时,我当然会有一个可用的 UrlHelper 实例 - 但这对我来说似乎有点老套。
I am using quartz.net to schedule regular events within asp.net mvc application.
The scheduled job should call a service layer script that requires a UrlHelper instance (for creating Urls based on correct routes (via urlHelper.Action(..)
) contained in emails that will be sent by the service).
I do not want to hardcode the links into the emails - they should be resolved using the urlhelper.
The job:
public class EvaluateRequestsJob : Quartz.IJob
{
public void Execute(JobExecutionContext context)
{
// where to get a usable urlHelper instance?
ServiceFactory.GetRequestService(urlHelper).RunEvaluation();
}
}
Please note that this is not run within the MVC pipeline. There is no current request being served, the code is run by the Quartz scheduler at defined times.
How do I get a UrlHelper instance usable on the indicated place?
If it is not possible to construct a UrlHelper, the other option I see is to make the job "self-call" a controller action by doing a HTTP request - while executing the action I will of course have a UrlHelper instance available - but this seems a little bit hacky to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为 UrlHelpler 创建一个新的 HttpContext 怎么样 答案:
How about just creating a new HttpContext for the UrlHelpler as in this answer:
编辑:抱歉,我完全误读了我猜的问题。
听起来您的调度程序(我不知道它是如何工作的)是一个单独的进程,并且您希望 UrlHelper 帮助您在 MVC 应用程序中生成有效的 URL?
您可以尝试在 MVC 应用程序中编写一个处理程序,该处理程序将在您的应用程序上下文中运行,为您构建 URL 并返回它。然后,您可以从调度程序中调用处理程序,以根据传入的参数获取所需的任何 URL。这样,您的调度程序只需要知道 MVC 应用程序的查询 URL 在哪里,然后就可以要求它执行 Url为您绘制地图。
希望这是一个更好的答案。如果我完全不在状态,请告诉我...本来打算删除我的回复,但我想我会再试一次。
Edit: Sorry I totally mis-read the question I guess.
It sounds like your scheduler (which I have no idea how it works) is a seperate process and you want the UrlHelper to help generate valid URLs in your MVC app?
You could try writing a handler in your MVC app that will be running under your applications context that will build the URL for you and return it. You could then call the handler from your scheduler to get any URL you need based on the params you pass in. This way your scheduler just needs to know about where the query URL of your MVC app is and then can ask it to do the Url mapping for you.
Hope this is a bit better of an answer. If I am totally off let me know... was going to delete my response but thought I would give it one more shot.
请记住在使用 UrlHelper.Action 方法时指定协议参数,这将生成绝对网址。示例:
或
Remember to specify the protocol parameter when using UrlHelper.Action method, this will generate absolute urls. Example:
or
您需要一个
RequestContext
来创建 UrlHelper。在我的 HtmlHelper 扩展方法之一中,我这样做:如何获取
RequestContext
取决于您的应用程序。You need a
RequestContext
to create a UrlHelper. In one of my HtmlHelper extension methods, I do it like this:How you get the
RequestContext
is dependent on your application.