You can do it as follows:
var urlBuilder = new System.UriBuilder(Request.Url.AbsoluteUri) { Path = Url.Action("Action", "Controller"), Query = null, }; Uri uri = urlBuilder.Uri; string url = urlBuilder.ToString();
Instead of Url.Action() in this example, you can also use Url.Content() or any routing method, or just simply go through the path.
But if the URL goes to Controller Action , there is a more compact way:
var contactUsUriString = Url.Action("Contact-Us", "About", routeValues: null , protocol: Request.Url.Scheme );
The trick here is that after specifying the protocol / scheme, when you call any routing method, you get an absolute URL. I recommend this when possible , but you also have a more general path in the first example, if you need one.
I talked about this in detail here:
http://gurustop.net/blog/2012/03/23/writing-absolute-urls-to-other-actions-in-asp-net-mvc/
Extracted from Meligys AngularJS and Web Dev Goodies Newsletter
Meligy Mar 23 '12 at 3:18 2012-03-23 ββ03:18
source share