嗨,我正在尝试添加一个锚,拉入域名,然后我可以有任何东西在那之后,例如
<a href="GET_THE_DOMAIN+/admin/pages/customers/add.aspx">ADD CUSTOMERS</a>我怎样才能在不硬编码的情况下获得域名,因为我们将使用相同的网站,但在不同的域名中有不同的内容,换句话说,唯一变化的是域名
谢谢
发布于 2012-02-07 23:58:39
你不能只使用没有域名的绝对链接吗?
<a href="/admin/pages/customers/add.aspx">ADD CUSTOMERS</a>发布于 2012-02-08 00:00:32
您可以创建一个所有aspx页都继承的基页,并在该基页中添加以下函数:
public string RootUrl(bool includeAppPath = false)
{
var context = HttpContext.Current;
var port = context.Request.ServerVariables["SERVER_PORT"];
if (port == null || port == "80" || port == "443")
{
port = "";
} else
{
port = ":" + port;
}
var protocol = context.Request.ServerVariables["SERVER_PORT_SECURE"];
if (protocol == null || protocol == "0")
{
protocol = "http://";
}else
{
protocol = "https://";
}
var appPath = "";
if (includeAppPath)
{
appPath = context.Request.ApplicationPath;
if (appPath == "/")
appPath = "";
}
var sOut = protocol + context.Request.ServerVariables["SERVER_NAME"] + port + appPath + "/";
return sOut;
}然后在你的锚标签中,你可以像下面这样调用这个函数:
<a href='<%= RootUrl() %>/admin/pages/customers/add.aspx'>ADD CUSTOMERS</a>发布于 2012-02-08 00:04:25
您可以使用以下内容:
<a href="<%=Request.Url.Host%>/admin/pages/customers/add.aspx">ADD CUSTOMERS</a>
话虽如此,为什么不能只使用没有域名的绝对URL呢?
https://stackoverflow.com/questions/9179411
复制相似问题