Get URL from code behind

During the development I faced this problem, but because I have no time to spend to see how to accomplish this, I met for simplicity in my web.config file a key with the right link. But now I have some time to revise the code and adjusted it. The situation is the following: on one server the application is configured to use the following URL in IIS

http://localhost/DOD.DefaultWebApp/Information.aspx

on another server :

http://www.dod.com/Information.aspx

Now how to accomplish this without using a key in web config file where is stipulated the correct URL address.
The solution is pretty simple, I solved it like this:

string requestedPage = Request.Url.Scheme + "://" + Request.Url.Authority.TrimEnd(Convert.ToChar("/")) + ResolveUrl(LinksWebApp.Information);

this also may be used in aspx, for example:

<script type="text/javascript">
window.location='<%=Request.Url.Scheme + "://" + Request.Url.Authority.TrimEnd(Convert.ToChar("/")) + ResolveUrl(LinksWebApp.Information);%>';
</script>

where:

public static class LinksWebApp
{
    public static string Information = "~/Information.aspx";
}

NOTE:
1) I used the syntax :
      Request.Url.Authority.TrimEnd(Convert.ToChar("/"))
   instead of
      Request.Url.Authority.TrimEnd('/')
  not because I love conversions :-), but it suits the case when we are using this syntax in the js like with window.location='...';
2) You may adjust with string.Format and other techniques to avoid string invariation, but for simplicity I showed you this  simple code.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted @ Wednesday, November 26, 2008 2:05 AM

Comments on this entry:

No comments posted yet.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: