所以...我有一个业务对象/经理,它将生成电子邮件。
这些电子邮件将包含网站上各种内容的链接...因此需要了解 MVC 路由...或至少如何为网站生成 URL...
但是我的业务对象将无法访问 RequestContext 等,并且电子邮件生成不一定是对网站的 Web 请求的结果(我有一个调度程序在将生成电子邮件的后台线程上运行)
关于如何在无法访问请求的情况下生成我的 url 的任何想法 - 因此无法使用 URLHelper...
想法?
最佳答案
为了获取 Controller 外部的 UrlHelper,您需要将其和路由数据提供给 HttpContext。这是一个例子:
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
HttpContextBase context = new HttpContextWrapper(HttpContext.Current);
UrlHelper helper = = new UrlHelper(new RequestContext(context, RouteTable.Routes.GetRouteData(context)));
关于model-view-controller - 在管理层中从 MVC 路由生成 urls...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1082748/