C# 不支持给定路径的格式。从Asp.net Api下载文件时出错

C# 不支持给定路径的格式。从Asp.net Api下载文件时出错,c#,asp.net-web-api,C#,Asp.net Web Api,在这里,我编写了从WebApi下载Excel表的简单c代码 但在下载过程中,由于不支持给定路径的格式,im出现错误。 请帮帮我 public HttpResponseMessage GetTestFile() { HttpResponseMessage result = null; // var localFilePath = HttpContext.Current.Server.MapPath(@"~/C:/Temp/3c575e

在这里,我编写了从WebApi下载Excel表的简单c代码 但在下载过程中,由于不支持给定路径的格式,im出现错误。 请帮帮我

 public HttpResponseMessage GetTestFile()
        {
            HttpResponseMessage result = null;
         //   var localFilePath = HttpContext.Current.Server.MapPath(@"~/C:/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0");
           var localFilePath = HttpContext.Current.Server.MapPath(@"~/C:/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0");


            if (!File.Exists(localFilePath))
            {
                result = Request.CreateResponse(HttpStatusCode.Gone);
            }
            else
            {
                // Serve the file to the client
                result = Request.CreateResponse(HttpStatusCode.OK);
                result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
                result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                result.Content.Headers.ContentDisposition.FileName = "SampleImg";
            }

            return result;
        }

MapPath仅适用于虚拟目录中的文件,而不适用于项目外部的文件。因此,请始终将需要的文件包含到项目中,然后使用mappath。var localFilePath=HttpContext.Current.Server.MapPath~/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0


mscorlib.dll中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理//这将在根目录中找到Example.txt文件。//。。。这可以在应用程序中任何目录的文件中使用。字符串localFilePath=HttpContext.Current.Request.MapPath~/Example.txt;。有主意吗?那么,在你的例子中,3c575edb-bf53-470c-a8b6-aa4234e796c0是一个文件夹吗?它存在于C:/Temp文件夹中,名为3c575edb-bf53-470c-a8b6-aa4234e796c0。你的例子对我帮助很大,但这里有关于HeaxaDecimal文件路径的想法吗?:=欢迎,很高兴为你服务。
localFilePath //it will contain your_project_path/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0
e.g: F:\projectname\Temp\3c575edb-bf53-470c-a8b6-aa4234e796c0