如何获取静态字符串的相对路径?

How to get relative path to static string?

在我的 Asp.Net 页面中,我有一个网络方法可以将一些数据发送到其他页面,对于这种方法,我必须提供路径,如下所示:

string Path => Server.MapPath(@"\myPath1");

[System.Web.Services.WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetAvailability()
{
    //xdocDetail = XDocument.Load(Path); //THIS DO NOT WORK, NEEDS STATIC STRING
    xdocDetail = XDocument.Load(@"C:\Users\Eggii\Source\Repos\adm_app3\ADM_App\Content\Xmls\Detail.xml");

    List<DetailList> availabilityList = (from detail in xdocDetail.Descendants("Product")
                                         select new DetailList
                                         {
                                          Detail6 = (string)detail.Element("Availability") ?? "Unknown",
                                         }).ToList();

    return JsonConvert.SerializeObject(availabilityList.Select(x => x.Detail6));
}

但我不想在我的方法中出现丑陋的完整路径,是否有可能以某种方式获得此 web 方法的相对路径?或者其他一些更好的解决方案?

如果您的内容存储在您的项目中,您可以使用 AppDomain.CurrentDomain.BaseDirectory,其中包含 ASP.NET 中的应用程序根目录。这看起来像:

xdocDetail = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory+Path)