Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting virtual path to actual web path in ASP.NET

Tags:

asp.net

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.

like image 989
stephenbayer Avatar asked Dec 09 '08 15:12

stephenbayer


People also ask

Which of the following MVC methods converts a virtual path to a physical path?

MapPath method is used to map the virtual path to a physical path.

What is virtual path and physical path in asp net?

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.

Where is virtual directory path in asp net?

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.

Is a physical path but a virtual path was expected C#?

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.


2 Answers

System.Web.VirtualPathUtility.ToAbsolute("yourRelativePath"); 

There you go :)

like image 184
Boris Callens Avatar answered Sep 21 '22 10:09

Boris Callens


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

like image 41
Rohit Agarwal Avatar answered Sep 21 '22 10:09

Rohit Agarwal