开发者

Proper root path for asp.net application (in dev and on the server)

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 se开发者_如何学运维rver, 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>";
        }


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


add this before URL ResolveUrl("~");


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜