Best practice: relative url

What's the best way / best way to get the url relative to the current page?

string url = Request.ApplicationPath + "/MyFolder/MyFile.aspx";

      

or

string url = Page.ResolveURL("~/MyFolder/MyFile.aspx");

      

I am using the previous method, but there was previously a question that was posted earlier on the Relative path from root operator ("~") in the code behind , which made me wonder if what I was doing was the best way.

+2


source to share


2 answers


I prefer to use

string url = Page.ResolveURL("~/MyFolder/MyFile.aspx");

      



But often you can only use "~ / MyFolder / MyFile.aspx" (HyperLink.NavigateUrl, Response.Redirect (), etc.).

I don't think it's better / worse anyway, it's just a preference. I think it is more important that you use the same method consistently.

+2


source


I use:

string url = string.Format( "{0}/Folder/SubFolder/File.aspx", ResolveClientURL( "~" ) );

      



I believe this is the simplest and most reliable method.

0


source







All Articles