How do I get the complete Url from the Html Helper class I created?

I have a html helper library that I am building and one of my plugins needs URLs to be passed. I don't want to pass the full url as they change something around every time. I need to go and fix all urls.

How can I get the complete url in my file? As if I was walking a relative path or something that it resolved to a full path.

+2


source to share


3 answers


VirtualPathUtility can be a place to look. For example using

VirtualPathUtility.ToAbsolute(src);

      



will pass paths like "~ / App / test.jpg" to an absolute location like "/VirtualDirectory/App/test.jpg" as well as relative paths. Methods exposed on an instance of the UrlHelper class (such as Content) can also be helpful.

+1


source


For future visitors to this topic, I often use the following code



var baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
if (HttpContext.Current.Request.Url.LocalPath != "/")
    baseUrl = baseUrl.Replace(HttpContext.Current.Request.Url.LocalPath.Substring(1), "");

      

+1


source


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

0


source







All Articles