Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How to create a usable UrlHelper instance?

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.

like image 772
Marek Avatar asked Mar 09 '10 21:03

Marek


2 Answers

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

like image 191
Duncan Avatar answered Nov 13 '22 06:11

Duncan


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.

like image 21
Kelsey Avatar answered Nov 13 '22 05:11

Kelsey