开发者

Get absolute url using routing

This should be simple. 开发者_运维知识库I want to get an absolute Uri given the controller, action and other routevalues. In other words I want to do what Html.ActionLink does but without the anchor and I want to do it in my controller.


When I tried to use UrlHelper(string action, string controller) I was still getting a relative path.

So to generate the full url I used a Uri and passed the Uri of the Request as the baseUri.

ie:


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


You can generate link URLs with UrlHelper.Action(). Your controllers already have this as property Url.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜