Reputation: 85845
I have an html helper library that I am making and one of my plugins needs urls to be passed in. I don't want to pass in the full url since they every-time I change something around I have to go and fix all of the urls up.
How can I get a full Url path in my file? Like if I pass in a relative path or something it gets resolved to a full path.
Upvotes: 4
Views: 3101
Reputation: 166
For future visitors to this thread I use the following code frequently
var baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
if (HttpContext.Current.Request.Url.LocalPath != "/")
baseUrl = baseUrl.Replace(HttpContext.Current.Request.Url.LocalPath.Substring(1), "");
Upvotes: 1
Reputation: 8755
VirtualPathUtility might be a place to look. For example using
VirtualPathUtility.ToAbsolute(src);
will render paths like "~/App/test.jpg" to an absolute location e.g "/VirtualDirectory/App/test.jpg" as well as relative paths. The methods exposed on an instance of the UrlHelper (e.g. Content) class might also be of help.
Upvotes: 3