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

C# 如何获取静态字符串的相对路径?,c#,path,static-methods,webmethod,C#,Path,Static Methods,Webmethod,在我的Asp.Net页面中,我有一个webmethod将一些数据发送到另一个页面,对于此方法,我必须提供路径,如下所示: string Path => Server.MapPath(@"\myPath1"); [System.Web.Services.WebMethod] [ScriptMethod(UseHttpGet = true)] public static string GetAvailability() { //xdocDetail = XDocument.Load(

在我的Asp.Net页面中,我有一个webmethod将一些数据发送到另一个页面,对于此方法,我必须提供路径,如下所示:

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));
}
stringpath=>Server.MapPath(@“\myPath1”);
[System.Web.Services.WebMethod]
[脚本方法(UseHttpGet=true)]
公共静态字符串GetAvailability()
{
//xdocDetail=XDocument.Load(Path);//这不起作用,需要静态字符串
xdocDetail=XDocument.Load(@“C:\Users\Eggii\Source\Repos\adm_app3\adm_App\Content\Xmls\Detail.xml”);
List availabilityList=(来自xdocDetail.subjections(“产品”)中的详细信息)
选择新的详细信息列表
{
Detail6=(字符串)detail.Element(“可用性”)??“未知”,
}).ToList();
返回JsonConvert.SerializeObject(availabilityList.Select(x=>x.Detail6));
}

但我不想在我的方法中使用那个丑陋的完整路径,是否有可能以某种方式获得这个webmethod的相对路径?或者其他更好的解决方案?

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


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

但如果内容未存储在项目中怎么办@马克斯Mokrousov@Eggii,您所说的内容未存储在项目中是什么意思?路径是相对于某事物的还是总是硬编码的?如果它总是硬编码的,那么除了存储完整路径之外,没有其他方法。但是,我建议将路径存储在web.config中,您可以在不更改代码的情况下更改路径。