1

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.

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

2 Comments

Great. The UrlHelper needed a RequestContext which I got from ControllerContext.
You don't have to specify AccountController as aAccount would suffice, as in 'Url.Action("Logout", "Account")'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.