URL '本地主机:端口/' ASP.NET 网站的问题
我正在开发一个应用程序,并且以编程方式生成菜单,因为我正在本地计算机上工作,我的网站路径就像
http ://localhost:55954/VirtualPathName/Default.aspx
下面是从数据库生成菜单的代码代码
String sHost, sHostWithPort, sAbsolutePath, sApplicationPath,sAbsoluteUri,sPathAndQuery,sPageID, sLangCode;
sHost = "http://"+HttpContext.Current.Request.Url.Host;
sHostWithPort = "http://" + HttpContext.Current.Request.Url.Authority;
sAbsolutePath = HttpContext.Current.Request.Url.AbsolutePath;
sApplicationPath = HttpContext.Current.Request.ApplicationPath;
sAbsoluteUri = HttpContext.Current.Request.Url.AbsoluteUri;
sPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
//Create Top Menu
StringBuilder sb = new StringBuilder();
if (dsMenu != null && dsMenu.Tables.Count > 0 && dsMenu.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsMenuRowCount; i++)
{
sb.Append("<div class='divlink' href='" + dsMenu.Tables[0].Rows[i]["PageInternalLinkURL"] + "?PageId=" + dsMenu.Tables[0].Rows[i]["PageId"] + "&Language=" + sLangCode + "'>" + dsMenu.Tables[0].Rows[i]["PageName"] + "</a></div>");
}
}
ltrMenu.Text = sb.ToString();
工作正常,除了当我必须将代码上传到我有的网络服务器时要将 for 循环中的以下语句修改为,
sb.Append("<div class='divlink' href='" + sHostWithPort + dsMenu.Tables[0].Rows[i]["PageInternalLinkURL"] + "?PageId=" + dsMenu.Tables[0].Rows[i]["PageId"] + "&Language=" + sLangCode + "'>" + dsMenu.Tables[0].Rows[i]["PageName"] + "</a></div>"
我必须从网站 URL 中取出 sApplicationPath ,以便 URL 类似于
http://www.xyz.com/Default.aspx
我搜索解决这个问题,但到目前为止我还无法修复。感谢您对代码的任何帮助,这样我在将文件上传到网络服务器上时就不必更改路径。
注意:-我太蠢了。我找到了解决办法。谢谢大家,
我刚刚删除了以下部分。+ sHostWithPort + sApplicationPath + "/"
谢谢大家,请不要将其标记为 - 有时我们倾向于这样做错误。我不知道如何删除问题。
I am working on a application and i programatically generate menus since i am working on the local machine my website path is like
http://localhost:55954/VirtualPathName/Default.aspx
Below is the code for generating menus from the database
String sHost, sHostWithPort, sAbsolutePath, sApplicationPath,sAbsoluteUri,sPathAndQuery,sPageID, sLangCode;
sHost = "http://"+HttpContext.Current.Request.Url.Host;
sHostWithPort = "http://" + HttpContext.Current.Request.Url.Authority;
sAbsolutePath = HttpContext.Current.Request.Url.AbsolutePath;
sApplicationPath = HttpContext.Current.Request.ApplicationPath;
sAbsoluteUri = HttpContext.Current.Request.Url.AbsoluteUri;
sPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
//Create Top Menu
StringBuilder sb = new StringBuilder();
if (dsMenu != null && dsMenu.Tables.Count > 0 && dsMenu.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsMenuRowCount; i++)
{
sb.Append("<div class='divlink' href='" + dsMenu.Tables[0].Rows[i]["PageInternalLinkURL"] + "?PageId=" + dsMenu.Tables[0].Rows[i]["PageId"] + "&Language=" + sLangCode + "'>" + dsMenu.Tables[0].Rows[i]["PageName"] + "</a></div>");
}
}
ltrMenu.Text = sb.ToString();
Code works fine except that when ever i have to upload the code to the web server i have to modify the following statement in for loop to
sb.Append("<div class='divlink' href='" + sHostWithPort + dsMenu.Tables[0].Rows[i]["PageInternalLinkURL"] + "?PageId=" + dsMenu.Tables[0].Rows[i]["PageId"] + "&Language=" + sLangCode + "'>" + dsMenu.Tables[0].Rows[i]["PageName"] + "</a></div>"
I have to take out the sApplicationPath from the website URL so that URL will be like
http://www.xyz.com/Default.aspx
I search to get around this problem but so far i have not been able to fix. Any help is appreciated with the code so that i don''t have to change the path when i upload the file on the web server.
NOTE: - So dumb of me. I found the solution . Thanks Guys
I just removed the following part.+ sHostWithPort + sApplicationPath + "/"
Thanks guys and please don't mark it as -ve some time we tend to make such mistake. I am not sure how to remove the Question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚将 URL 设置为相对 URL,
我刚刚删除了以下部分。+ sHostWithPort + sApplicationPath + "/"
谢谢大家,请不要将其标记为 - 有时我们往往会犯这样的错误。我不知道如何删除问题。
I just made the URL as relative URL
I just removed the following part.+ sHostWithPort + sApplicationPath + "/"
Thanks guys and please don't mark it as -ve some time we tend to make such mistake. I am not sure how to remove the Question.