[HttpGet] public IHttpActionResult Parse() { var sPath = @"C:Users梁斌source eposWebApplication2WebApplication2ImagesBodyPart_3fcd168b-3849-4dd6-965e-690568901522.png"; //var sPath = @"~TempDataabc.doc"; if (sPath.IndexOf("~") == 0) sPath = HostingEnvironment.MapPath(sPath); //转为绝对路径 //取得当前网站的绝对路径 var sRootePath = HostingEnvironment.MapPath(HostingEnvironment.ApplicationVirtualPath); //取得文件相对于网站的路径(如:TempData/abc.mdb) var sRelativeUri = new Uri(sRootePath, UriKind.Absolute).MakeRelativeUri(new Uri(sPath, UriKind.Absolute)).ToString(); // 先判断是否与原路径相同,如相同则表示二者可能不在同一个磁盘上 var sResult = $"http://{HttpContext.Current.Request.Url.Authority}/{sRelativeUri}"; //构造返回路径 return Json(sResult); }