views:

971

answers:

2

Here's the scenario. We have a router that port forwards requests for our different test sites. For example http://www.ourSite.com:8051 forwards from the router to a web server that is on port 80. The test web sites are virtual directories of one web site running on IIS6 (Windows Server 2003).

Part of our application send out e-mails that use a base url to build some links in the application. Here's the base url being built in our on application start:

siteBaseUrl = string.Format("{0}://{1}{2}",
                HttpContext.Current.Request.Url.Scheme,
                HttpContext.Current.Request.Url.Authority,
                HttpContext.Current.Request.ApplicationPath.TrimEnd('/'));

So let's say one of our tests site's looks like this, http://www.ourSite.com:8051/client1. I would expect siteBaseUrl to look like this, http://www.ourSite.com:8051/client1, when in fact it ends up looking like this http://www.ourSite.com/client1 (I tried finding a question like this on StackOverflow but found none. Maybe I wasn't searching with the right keywords, or I need more coffee. Any help on this matter would be greatly appreciated.

A: 

If the port number is getting stripped away by your router, you could always use javascript to grab the url and just send it to the server.

rogueg
Not an option as this is running in the Application_Start method. As well this sounds too hacky. I need server-side guaranteedness (not a word, but sounds good) that the url is set properly.
nickyt
+1  A: 

The HttpContext.Current.Request.Url is based on the resource requested from IIS, it doesn't represent the Client request Url. You can however use the Host property from the HTTP header.

siteBaseUrl = string.Format("{0}://{1}{2}",
                HttpContext.Current.Request.Url.Scheme,
                HttpContext.Current.Request.Headers["host"],
                HttpContext.Current.Request.ApplicationPath.TrimEnd('/'));
CptSkippy
+1 - This would be the only way I know of to get the port
John Rasch