Creating URLs Independent of Controllers in ASP.NET MVC 3

I have an mvc 3 application with a background thread checking the status of some database items. When it finds an expired item, it sends an email. In an email, I would like to provide a url to call the call to view the status. If this was done from a controller, I would use UrlHelper, for example:

string body_url = "For more details see: " + Url.Action("Details", "MyOrder", new { OrderId = order.OrderId }, Constants.HttpProtocol);

However, I am not in the controller, and my method is not called from the controller, it starts when the application starts. Is there a way to generate a valid UrlHelper or, if not, generate a valid URL without resorting to hard coding, regardless of the controllers?

+5
source share
1 answer

... ref msdn ... :)

var request = new HttpRequest("/", "http://example.com", ""); //hopefully you can hardcode this or pull from config?
var response = new HttpResponse(new StringWriter());
var httpContext = new HttpContext(request, response);

var httpContextBase = new HttpContextWrapper(httpContext);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContextBase, routeData);

var urlHelper = new UrlHelper(requestContext);
var url = urlHelper.Action("ActionName", "ControllerName");
+4

All Articles