How to generate Preview Item's URL In IScheduledTaskHandler.Process

Topics: Customizing Orchard
Jan 7, 2015 at 3:22 AM
Hi

I write a IScheduledTaskHandler to monitor expire workflow activity. I want to send mail to embeded with preview item URL. I have googled, there are one class UrlHelper. But I can't get
RequestContext in IScheduledTaskHandler.Process().

var urlHelper = new UrlHelper(_services.WorkContext.HttpContext.Request.RequestContext);
string scheme = _services.WorkContext.HttpContext.Request.Url.Scheme;
string previewUrl = urlHelper.Action("Preview", "Item",
                new { area = "Contents", id = docPart.ContentItem.Id }, scheme);
Thanks!
Jan 7, 2015 at 7:51 AM
Hi

I googled and summarize a solution, but not best:
            var httpRequest = new HttpRequest("/", _services.WorkContext.CurrentSite.BaseUrl, "");
            var httpResponse = new HttpResponse(new StringWriter());
            var httpContext = new HttpContext(httpRequest, httpResponse);
            var httpContextBase = new HttpContextWrapper(httpContext);

            var virtualRequestContext = new RequestContext(httpContextBase, new RouteData());
            var urlHelper = new UrlHelper(virtualRequestContext);
            string scheme = "http";
            string previewUrl = urlHelper.Action("Preview", "Item",
                new { area = "Contents", id = docPart.ContentItem.Id }, scheme);
I can't get scheme, so hard code as "http", are there available to get scheme without HttpContext
Jan 7, 2015 at 8:04 AM
Edited Jan 7, 2015 at 8:28 AM
In your context, even without RequestContext, you can use an UrlHelper but as a dependency injection. You will get an UrlHelper with an HttpContext based on your site baseUrl. So, you need to set correctly the baseUrl in the site settings (personnally, I use an IActionFilter to update automatically this baseUrl)

Declare
private readonly UrlHelper _urlHelper;
In your constructor
...
, UrlHelper urlHelper) {
_urlHelper = urlHelper;
....
And use it

Regards

Update:

To get the scheme
var scheme = new Uri(_orchardServices.WorkContext.CurrentSite.BaseUrl, UriKind.Absolute).Scheme;
Note: Add a null or empty checking on the baseUrl...
You can also test this
Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute)
Regards
Jan 7, 2015 at 8:23 AM
Hi

Update:
            var httpRequest = new HttpRequest("/", _services.WorkContext.CurrentSite.BaseUrl, "");
            var httpResponse = new HttpResponse(new StringWriter());
            var httpContext = new HttpContext(httpRequest, httpResponse);
            var httpContextBase = new HttpContextWrapper(httpContext);

            var virtualRequestContext = new RequestContext(httpContextBase, new RouteData());
            var urlHelper = new UrlHelper(virtualRequestContext);
            string scheme = virtualRequestContext.HttpContext.Request.Url.Scheme;
            string previewUrl = urlHelper.Action("Preview", "Item",
                new { area = "Contents", id = docPart.ContentItem.Id }, scheme);
Marked as answer by ezrachen on 1/7/2015 at 12:23 AM
Jan 7, 2015 at 8:47 AM
Try to use an UrlHelper but by using dependency injection, normally an HttpContext (based on the baseUrl) will have been created for you