在ASP.NET MVC 3中生成独立于控制器的URL

Sim*_*ker 5 c# urlhelper asp.net-mvc-3

我有一个mvc 3应用程序,后台线程检查一些数据库项的状态.当它找到过期的项目时,它会发送一封电子邮件.在电子邮件中,我想要包含要呼叫的操作的URL以查看状态.如果这是从控制器完成的,我会使用UrlHelper,例如:

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

但是,我不在控制器中,也不是从控制器调用我的方法,它是在应用程序启动时启动的.有没有办法生成有效的UrlHelper,或者,如果没有,生成有效的URL,而不依赖于硬编码路径,独立于控制器?

fel*_*ckz 4

这是理论上的...参考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");
Run Code Online (Sandbox Code Playgroud)