Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#中的DLL中找到基本url?_C#_.net_Asp.net_Url_Web Applications - Fatal编程技术网

如何从C#中的DLL中找到基本url?

如何从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

在C#.NET web应用程序调用的DLL中,如何查找web应用程序的基本url?

您可以使用Assembly.GetExecutionGassembly()获取DLL的Assembly对象

然后,调用Server.MapPath,传入该程序集的完整路径以获取本地根路径。

这是否可行

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的这一部分做了一些事情。你节省了我的时间!!