Search code examples
c#asp.netvirtual-directory

URL 'localhost:port/' issue for asp.net website


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.


Solution

  • 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.