Gat*_*ler 55
也许这可能会奏效:
String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
Run Code Online (Sandbox Code Playgroud)
我正在使用c#但可以适应vb.
Can*_*oas 37
使用Server.RelativePath(路径)不是很好吗?
好吧,你只需要扩展它;-)
public static class ExtensionMethods
{
public static string RelativePath(this HttpServerUtility srv, string path, HttpRequest context)
{
return path.Replace(context.ServerVariables["APPL_PHYSICAL_PATH"], "~/").Replace(@"\", "/");
}
}
Run Code Online (Sandbox Code Playgroud)
有了它,你可以简单地打电话
Server.RelativePath(path, Request);
Run Code Online (Sandbox Code Playgroud)
Ale*_*use 13
我知道这是旧的,但我需要考虑虚拟目录(根据@Costo的评论).这似乎有助于:
static string RelativeFromAbsolutePath(string path)
{
if(HttpContext.Current != null)
{
var request = HttpContext.Current.Request;
var applicationPath = request.PhysicalApplicationPath;
var virtualDir = request.ApplicationPath;
virtualDir = virtualDir == "/" ? virtualDir : (virtualDir + "/");
return path.Replace(applicationPath, virtualDir).Replace(@"\", "/");
}
throw new InvalidOperationException("We can only map an absolute back to a relative path if an HttpContext is available.");
}
Run Code Online (Sandbox Code Playgroud)
我喜欢卡诺阿斯的想法。不幸的是,我没有可用的“ HttpContext.Current.Request”(BundleConfig.cs)。
我这样改变了方法:
public static string RelativePath(this HttpServerUtility srv, string path)
{
return path.Replace(HttpContext.Current.Server.MapPath("~/"), "~/").Replace(@"\", "/");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
55508 次 |
最近记录: |