ASP.NET MVC:如何创建可用的 UrlHelper 实例?

发布于 2024-08-24 11:53:12 字数 700 浏览 7 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北座城市 2024-08-31 11:53:12

为 UrlHelpler 创建一个新的 HttpContext 怎么样 答案

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

情徒 2024-08-31 11:53:12

编辑:抱歉,我完全误读了我猜的问题。

听起来您的调度程序(我不知道它是如何工作的)是一个单独的进程,并且您希望 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.

倾城花音 2024-08-31 11:53:12

请记住在使用 UrlHelper.Action 方法时指定协议参数,这将生成绝对网址。示例:

url.Action("Action", "Controller", null, "http")

url.Action("Action", "Controller", null, request.Url.Scheme)

Remember to specify the protocol parameter when using UrlHelper.Action method, this will generate absolute urls. Example:

url.Action("Action", "Controller", null, "http")

or

url.Action("Action", "Controller", null, request.Url.Scheme)
东京女 2024-08-31 11:53:12

您需要一个 RequestContext 来创建 UrlHelper。在我的 HtmlHelper 扩展方法之一中,我这样做:

public static string ScriptUrl(this HtmlHelper html, string script)
{
    var url = new UrlHelper(html.ViewContext.RequestContext);
    ...
}

如何获取 RequestContext 取决于您的应用程序。

You need a RequestContext to create a UrlHelper. In one of my HtmlHelper extension methods, I do it like this:

public static string ScriptUrl(this HtmlHelper html, string script)
{
    var url = new UrlHelper(html.ViewContext.RequestContext);
    ...
}

How you get the RequestContext is dependent on your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文