这确实需要参考System.Web.MVC
,但它应该让您对如何完成它有一个基本的了解。我创建了一个扩展来帮助我解决这个问题:
public static string GetAbsoluteURL(this RouteCollection routes, RequestContext context, RouteValueDictionary values, HttpProtocolType httpProtocol)
{
string host;
if (context.HttpContext.Request.Url != null)
{
host = context.HttpContext.Request.Url.Authority;
}
else
{
host = context.HttpContext.Request.UrlReferrer.Host;
}
string virtualPath = routes.GetVirtualPath(context, "Default", values).VirtualPath;
string protocol = httpProtocol == HttpProtocolType.HTTP ? "http" : "https";
return string.Format("{0}://{1}{2}", protocol, host, virtualPath);
}