2010-02-20 95 views
1

这应该很简单。我想得到一个绝对的乌里给予控制器,行动和其他路线的价值。换句话说,我想要做什么Html.ActionLink做,但没有锚点,我想在我的控制器。使用路由获取绝对网址

回答

0

您可以使用UrlHelper.Action()生成链接网址。你的控制器已经拥有这个属性Url。

Url.Action("Logout", "AccountController") 
+0

大。 UrlHelper需要一个我从ControllerContext获得的RequestContext。 – Martin

+0

您不必指定AccountController作为aAccount就足够了,如'Url.Action(“Logout”,“Account”)' –

4

当我尝试使用UrlHelper(字符串操作,字符串控制器)时,我仍然得到一个相对路径。

因此,要生成完整的网址,我使用了Uri并将请求的URI作为baseUri传递。

即:


var urlHelper = new UrlHelper(Request.RequestContext); 
var routeUri = new Uri(Request.Url, urlHelper.Action("action", "controller")); 
return routeUri.AbsoluteUri;