سؤال

إذا تمكنت من تحديد موقع ملف والتحقق من وجوده باستخدام Server.MapPath وأريد الآن إرسال المستخدم مباشرة إلى هذا الملف، فما هو الحل؟ الأسرع طريقة لتحويل هذا المسار المطلق مرة أخرى إلى مسار ويب نسبي؟

هل كانت مفيدة؟

المحلول

ربما قد ينجح هذا:

String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);

أنا أستخدم c# لكن يمكن تعديله إلى vb.

نصائح أخرى

لن يكون جميلا أن يكون 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(@"\", "/");
    }
}

مع هذا يمكنك ببساطة الاتصال

Server.RelativePath(path, Request);

أعلم أن هذا قديم ولكني بحاجة إلى حساب الدلائل الافتراضية (حسب تعليق @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.");
}

تعجبني الفكرة من كانواس.لسوء الحظ لم يكن "HttpContext.Current.Request" متاحًا (BundleConfig.cs).

لقد غيرت الطريقة هكذا:

public static string RelativePath(this HttpServerUtility srv, string path)
{
     return path.Replace(HttpContext.Current.Server.MapPath("~/"), "~/").Replace(@"\", "/");
}

إذا استخدمت Server.MapPath، فيجب أن يكون لديك بالفعل مسار الويب النسبي.بحسب ال وثائق MSDN, ، هذه الطريقة تأخذ متغير واحد، طريق, ، وهو المسار الظاهري لخادم الويب.لذا، إذا تمكنت من استدعاء الطريقة، فيجب أن يكون لديك بالفعل مسار الويب النسبي الذي يمكن الوصول إليه على الفور.

بالنسبة لـ asp.net core كتبت فئة مساعدة للحصول على المسارات في كلا الاتجاهين.

public class FilePathHelper
{
    private readonly IHostingEnvironment _env;
    public FilePathHelper(IHostingEnvironment env)
    {
        _env = env;
    }
    public string GetVirtualPath(string physicalPath)
    {
        if (physicalPath == null) throw new ArgumentException("physicalPath is null");
        if (!File.Exists(physicalPath)) throw new FileNotFoundException(physicalPath + " doesn't exists");
        var lastWord = _env.WebRootPath.Split("\\").Last();
        int relativePathIndex = physicalPath.IndexOf(lastWord) + lastWord.Length;
        var relativePath = physicalPath.Substring(relativePathIndex);
        return $"/{ relativePath.TrimStart('\\').Replace('\\', '/')}";
    }
    public string GetPhysicalPath(string relativepath)
    {
        if (relativepath == null) throw new ArgumentException("relativepath is null");
        var fileInfo = _env.WebRootFileProvider.GetFileInfo(relativepath);
        if (fileInfo.Exists) return fileInfo.PhysicalPath;
        else throw new FileNotFoundException("file doesn't exists");
    }

من وحدة التحكم أو الخدمة، قم بحقن FilePathHelper واستخدم:

var physicalPath = _fp.GetPhysicalPath("/img/banners/abro.png");

والعكس

var virtualPath = _fp.GetVirtualPath(physicalPath);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top