I have a virtual path (example: "~/Images/Banner.jpg") and I want to make that an absolute web path (example: "/ApplicationRoot/Images/Banner.jpg"). There is a method that will do this, I believe in a class called something like HTTPUtility or similar name. Though ever time I need this method, it takes me hours searching for it. It would be greatly appreciated if someone could post the proper method to do this so I can favorite this for easy access in the future.
Thank you very much.
MapPath method is used to map the virtual path to a physical path.
Physical path - This is the actual path the file is located by IIS. Virtual path - This is the logical path to access the file which is pointed to from outside of the IIS application folder.
Go to Websites & Domains and find the website's domain name. Click Virtual Directories. Browse to the required directory and click a link with its name. Click ASP.NET Settings.
Generally this physical and virtual path problem occurred whenever we refer “Server. MapPath” value multiple times while using folder path in applications. To solve this e:is a physical path but a virtual path was expected we need to use Server.
System.Web.VirtualPathUtility.ToAbsolute("yourRelativePath");
There you go :)
There are various ways that are available in ASP.NET that we can use to resolve relative paths to absolute Urls -
1) Request.ApplicationPath
2) System.Web.VirtualPathUtility
3) Page.ResolveUrl
4) Page.ResolveClientUrl
Here's a article that explains the difference between the various ways to resolving paths in ASP.NET -
Different approaches for resolving URLs in ASP.NET
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With