获取程序地址或路径

获取程序地址或路径

Web地址:

JS:

var baseUrl = window.location.host;//不带http://  结果为: localhost:8033

asp.net:

string host = HttpContext.Current.Request.Url.Host; string baseUrl = host+":"+port; //也不带 http://

region 获取web地址         {             var allurl = HttpContext.Current.Request.Url.AbsoluteUri;// "http://localhost:8047/api/getgoods                 url = "http://";             //var absolutePath = HttpContext.Current.Request.Url.AbsolutePath;// "/api/getgoods"             //var host = HttpContext.Current.Request.Url.Host;//"localhost"             url += serverpath+"/" ;         }
        #endregion

实际地址:

string FileDir=HttpContext.Current.Request.ApplicationPath.ToString();//根路径

 

Form:

string path = Application.StartupPath.ToString() + "\aa.exe";

 

Web 获取实际路径

1.controller中

Server.MapPath("/Plugin/layuifont/icon_data.json");

E:\source\suyuan\SuYuan.Web\Plugin\layuifont\icon_data.json

2.非Controller中获得文件物理路径

        /// <summary>         /// </summary>         public static string GetMapPath(string path)             if (HttpContext.Current != null)                 return HttpContext.Current.Server.MapPath(path);             else                 return System.Web.Hosting.HostingEnvironment.MapPath(path);         }

上一篇:C# Aspose.Words 实现Word文档导出


下一篇:ASP.NET Core管道详解[2]: HttpContext本质论