chobo2
chobo2

Reputation: 85845

How to get full Url from a Html Helper Class that I made?

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

Answers (3)

Cleiton
Cleiton

Reputation: 18133

You can use HttpContext.Current.Server.MapPath(string)

Upvotes: 0

Mike
Mike

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

John Foster
John Foster

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

Related Questions