If I have managed to locate and verify the existence of a file using Server.MapPath and I now want to send the user directly to that file, what is the fastest way to convert that absolute path back into a relative web path?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
If you used Server.MapPath, then you should already have the relative web path. According to the MSDN documentation, this method takes one variable, path, which is the virtual path of the Web server. So if you were able to call the method, you should already have the relative web path immediately accessible.
I know this is old but I needed to account for virtual directories (per @Costo's comment). This seems to help:
For asp.net core i wrote helper class to get pathes in both directions.
from Controller or service inject FilePathHelper and use:
and versa
Perhaps this might work:
I'm using c# but could be adapted to vb.
I like the idea from Canoas. Unfortunately I had not "HttpContext.Current.Request" available (BundleConfig.cs).
I changed the methode like this:
Wouldn't it be nice to have Server.RelativePath(path)?
well, you just need to extend it ;-)
With this you can simply call