Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 950
chobo2 Avatar asked Aug 31 '25 17:08

chobo2


2 Answers

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.

like image 104
John Foster Avatar answered Sep 02 '25 08:09

John Foster


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), "");
like image 31
Mike Avatar answered Sep 02 '25 07:09

Mike