asp.net 应用程序的正确根路径(在开发中和服务器上)

发布于 2024-11-09 02:52:20 字数 1375 浏览 2 评论 0原文

在我的个人开发环境中,我在它自己的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌酱 2024-11-16 02:52:21

在 URL 之前添加此内容 ResolveUrl("~");

add this before URL ResolveUrl("~");

娇俏 2024-11-16 02:52:21

当您使用服务器控件时,您可以使用 ~ 创建一个始终正确的相对路径(本地主机和服务器上):

<asp:HyperLink id="hyperlink1" NavigateUrl="~/EnrollmentLookup.aspx"
              Text="Enrollment Search" runat="server"/>     

如果您想使用 System.IO 访问物理设备,您可以调用 Server.MapPath

Server.MapPath("~/log/data.txt");

如果您需要一个您可以使用绝对路径,

VirtualPathUtility.ToAbsolute("~/EnrollmentLookup.aspx");

但我建议您使用 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):

<asp:HyperLink id="hyperlink1" NavigateUrl="~/EnrollmentLookup.aspx"
              Text="Enrollment Search" runat="server"/>     

In case you want to access physical devices using System.IO, you can call Server.MapPath

Server.MapPath("~/log/data.txt");

If you need an absolute path you can use

VirtualPathUtility.ToAbsolute("~/EnrollmentLookup.aspx");

But I would suggest, that you use a UserControl or a asp:Repeater to generate the HTML.

﹏雨一样淡蓝的深情 2024-11-16 02:52:20

这里的所有其他答案都是正确的,但我只是想添加一些内容。要执行他们的建议并动态返回 HTML 内容,您可以使用 HtmlGenericContro 类,然后您将在其中添加嵌套控件。

    HtmlGenericControl adminMenu = new HtmlGenericControl("ul");
    HtmlGenericControl liItem  = new HtmlGenericControl("li");
    HyperLink link = new HyperLink();

    link.NavigateUrl = "~EnrollmentLookup.aspx";
    liItem.Controls.Add(link);
    adminMenu.Controls.Add(liItem);

    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);

    adminMenu.RenderControl(hw);
    return sb.ToString();

尽管使用用户控件来定义 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.

    HtmlGenericControl adminMenu = new HtmlGenericControl("ul");
    HtmlGenericControl liItem  = new HtmlGenericControl("li");
    HyperLink link = new HyperLink();

    link.NavigateUrl = "~EnrollmentLookup.aspx";
    liItem.Controls.Add(link);
    adminMenu.Controls.Add(liItem);

    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);

    adminMenu.RenderControl(hw);
    return sb.ToString();

Although you may be even better off yet using a User Control to define your AdminMenu

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文