如何从C#中的DLL中找到基本url?
在C#.NET web应用程序调用的DLL中,如何查找web应用程序的基本url?您可以使用Assembly.GetExecutionGassembly()获取DLL的Assembly对象 然后,调用Server.MapPath,传入该程序集的完整路径以获取本地根路径。这是否可行如何从C#中的DLL中找到基本url?,c#,.net,asp.net,url,web-applications,C#,.net,Asp.net,Url,Web Applications,在C#.NET web应用程序调用的DLL中,如何查找web应用程序的基本url?您可以使用Assembly.GetExecutionGassembly()获取DLL的Assembly对象 然后,调用Server.MapPath,传入该程序集的完整路径以获取本地根路径。这是否可行 HttpContext.Current.Request.Url 更新: 要获取基本URL,可以使用: HttpContext.Current.Request.Url.GetComponents(UriComponen
HttpContext.Current.Request.Url
更新:
要获取基本URL,可以使用:
HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)
虽然我不确定这是否是最好的解决方案,但我还是想到了这个:
string _baseUrl = String.Empty;
HttpContext httpContext = HttpContext.Current;
if (httpContext != null)
{
_baseURL = "http://" + HttpContext.Current.Request.Url.Host;
if (!HttpContext.Current.Request.Url.IsDefaultPort)
{
_baseURL += ":" + HttpContext.Current.Request.Url.Port;
}
}
如果程序集可能被非web项目引用,那么您可能希望避免使用System.web命名空间
我会使用DannySmurf的方法。正如Alexander所说,您可以使用HttpContext.Current.Request.Url,但如果您不想使用http://:
HttpContext.Current.Request.Url.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);
你可以发布一个完整的URL和你想要什么的例子吗?我在寻找网站的URL而不是目录路径。谢谢谢谢Alexander-我利用HttpContext的这一部分做了一些事情。你节省了我的时间!!