我想动态获取ASP.NET应用程序的绝对根URL.这需要是以下形式的应用程序的完整根URL:http(s):// hostname(:port)/
我一直在使用这种静态方法:
public static string GetSiteRootUrl()
{
string protocol;
if (HttpContext.Current.Request.IsSecureConnection)
protocol = "https";
else
protocol = "http";
StringBuilder uri = new StringBuilder(protocol + "://");
string hostname = HttpContext.Current.Request.Url.Host;
uri.Append(hostname);
int port = HttpContext.Current.Request.Url.Port;
if (port != 80 && port != 443)
{
uri.Append(":");
uri.Append(port.ToString());
}
return uri.ToString();
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我没有HttpContext.Current
范围怎么办?我遇到过这样的情况CacheItemRemovedCallback
.
在Asp.Net中的共享/静态函数中使用ResolveUrl()的最佳方法是什么?我目前的VB.Net解决方案是:
Dim x As New System.Web.UI.Control
x.ResolveUrl("~/someUrl")
Run Code Online (Sandbox Code Playgroud)
或C#:
System.Web.UI.Control x = new System.Web.UI.Control();
x.ResolveUrl("~/someUrl");
Run Code Online (Sandbox Code Playgroud)
但我意识到这不是调用它的最佳方式.
我正在尝试这样做:
<a href="~/Cases/SupRequestSearch.aspx">Search request</a>
Run Code Online (Sandbox Code Playgroud)
所以我需要将~
其呈现为http://myserver/app/...
在mvc我会这样做
<a href="<%=Url.Content("~/Cases/SupRequestSearch.aspx")%>>Search request</a>
Run Code Online (Sandbox Code Playgroud)
asp.net网络表单中有类似的东西吗?