ASP.NET MVC:如何创建一个可用UrlHelper实例? [英] ASP.NET MVC: How to create a usable UrlHelper instance?

查看:167
本文介绍了ASP.NET MVC:如何创建一个可用UrlHelper实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用quartz.net安排asp.net的MVC应用程序中的定期活动。

I am using quartz.net to schedule regular events within asp.net mvc application.

计划的作业应该调用一个需要UrlHelper实例(服务层脚本基于包含在正确的路由(通过urlHelper.Action(..))创建的url将由服务发送的电子邮件)。

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

我不想硬code中的链接进入的邮件 - 他们应该使用urlhelper解决

I do not want to hardcode the links into the emails - they should be resolved using the urlhelper.

这项工作:

public class EvaluateRequestsJob : Quartz.IJob
{
    public void Execute(JobExecutionContext context)
    {
        //  where to get a usable urlHelper instance?
        ServiceFactory.GetRequestService(urlHelper).RunEvaluation();
    }
}

请注意,这不是对MVC管道内运行。有被服务在规定的时候,code是由Quartz调度运行无电流要求。

我如何得到指定的地方UrlHelper实例使用吗?

How do I get a UrlHelper instance usable on the indicated place?

如果这是不可能构建一个UrlHelper,其他选项我看到的是做一个HTTP请求,使工作自呼的控制器操作 - 在执行操作过程中,我将有一个可用的UrlHelper实例 - 但是这似乎有点哈克给我

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.

推荐答案

如何只创建一个新的HttpContext为UrlHelpler如本<一个href=\"http://stackoverflow.com/questions/2031995/call-urlhelper-in-models-in-asp-net-mvc/2032257#2032257\">answer:

How about just creating a new HttpContext for the UrlHelpler as in this answer:

这篇关于ASP.NET MVC:如何创建一个可用UrlHelper实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆