asp.net 应用程序的正确根路径(在开发中和服务器上)
在我的个人开发环境中,我在它自己的IP(127.0.0.2)上有一个asp.net应用程序,因此root始终是“/”
在服务器上,它位于一个应用程序中,它表现为“servername.com/appname/” “
我正在建立指向字符串中的文件的超链接,但似乎无法在服务器上正确解析文件名。他们提供了“servername.com”,但没有提供应用程序名称部分。
我缺少什么?
我调用一个 Web 帮助程序类(如下所示)来获取菜单的路径:
public static string getOnsiteAdministratorMenu()
{
string syllabusSearchURL = "/admin/syllabus_Search.aspx";
return "<ul id=\"nav\"><li><a href=\"EnrollmentLookup.aspx\">Enrollment Search</a></li>" +
"<li><a href=\"OnsiteEnrollment_AddNew.aspx\">Enrollment Add</a></li>" +
"<li><a href=\"ViewSingleDocument.aspx\">Document Search</a></li>" +
"<li><a href=\"/Admin/SyllabusAdd.aspx\">Add Syllabus</a></li>" +
"<li><a href=\"" + syllabusSearchURL + "\">Edit Syllabus</a></li>" +
"<li><a href=\"Course_add.aspx\">Add Course</a></li>" +
"<li><a href=\"Group_add.aspx\">Add Group</a></li>" +
"<li><a href=\"GroupSearch.aspx\">Groups</a></li>" +
"<li><a href=\"Admin/Admin_GradeChanges.aspx\">Audit Grade Changes</a></li></ul>";
}
In my personal dev environment, I have an asp.net application on it's own ip (127.0.0.2), thus root is always "/"
On the server, it's in an app, which comes across as "servername.com/appname/"
I'm building up hyperlinks to files in strings and can't seem to parse out the filenames properly on the server. They come out with the "servername.com" but not the appname part.
What am I missing?
I call a web helper class (shown below) to get the paths for a menu:
public static string getOnsiteAdministratorMenu()
{
string syllabusSearchURL = "/admin/syllabus_Search.aspx";
return "<ul id=\"nav\"><li><a href=\"EnrollmentLookup.aspx\">Enrollment Search</a></li>" +
"<li><a href=\"OnsiteEnrollment_AddNew.aspx\">Enrollment Add</a></li>" +
"<li><a href=\"ViewSingleDocument.aspx\">Document Search</a></li>" +
"<li><a href=\"/Admin/SyllabusAdd.aspx\">Add Syllabus</a></li>" +
"<li><a href=\"" + syllabusSearchURL + "\">Edit Syllabus</a></li>" +
"<li><a href=\"Course_add.aspx\">Add Course</a></li>" +
"<li><a href=\"Group_add.aspx\">Add Group</a></li>" +
"<li><a href=\"GroupSearch.aspx\">Groups</a></li>" +
"<li><a href=\"Admin/Admin_GradeChanges.aspx\">Audit Grade Changes</a></li></ul>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 URL 之前添加此内容
ResolveUrl("~");
add this before URL
ResolveUrl("~");
当您使用服务器控件时,您可以使用 ~ 创建一个始终正确的相对路径(本地主机和服务器上):
如果您想使用 System.IO 访问物理设备,您可以调用 Server.MapPath
如果您需要一个您可以使用绝对路径,
但我建议您使用 UserControl 或 asp:Repeater 来生成 HTML。
when you use server controls, you can use the ~ to create a relative path which is always correct (localhost and on server):
In case you want to access physical devices using System.IO, you can call Server.MapPath
If you need an absolute path you can use
But I would suggest, that you use a UserControl or a asp:Repeater to generate the HTML.
这里的所有其他答案都是正确的,但我只是想添加一些内容。要执行他们的建议并动态返回 HTML 内容,您可以使用 HtmlGenericContro 类,然后您将在其中添加嵌套控件。
尽管使用用户控件来定义 AdminMenu 可能会更好
All the other answers here are correct, but I just wanted to add something. To do what they suggest and dynamically return HTML content you can use the HtmlGenericContro class Then you would add the nested controls inside.
Although you may be even better off yet using a User Control to define your AdminMenu