2

I'm working through the final issues of an application set to go live this week. I need help to either modify my code or explain to our hosters what they need to fix in the IIS/DNS configurations to make this code work as expected.

Here is the code:

public string BaseSiteUrl
{
    get
    {
        var c = this.ControllerContext.RequestContext.HttpContext;
        string baseUrl = c.Request.Url.Scheme + "://" + c.Request.Url.Authority
                       + c.Request.ApplicationPath.TrimEnd('/') + '/';
        return baseUrl;
    }
}

I make a call to this in my Controller, to generate a url that gets persisted to a database.

It works fine when I run on my local machine. However, it does not work when it is run on the beta server.

Expected results on beta. On the beta server this is an application named dr405

  • https://beta.sc-pa.com/dr405/

The actual result on beta. (I changed the server/domain names to what you see in CAPS for security's sake)

  • http://SERVERNAME1.GROUP1.SUBGROUP.local/dr405/
Matthias
  • 15,919
  • 5
  • 39
  • 84
Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91

3 Answers3

1

I don't think you need the method you wrote. There is a UrlHelper class that adds extension methods. To get the base URL for your site you should be using the Content() method like this:

var baseUrl = Url.Content("~/");

In your example, it looks like the http://SERVERNAME1.GROUP1.SUBGROUP.local/dr405/ result is an internal host name. On your development machine the internal host matches your public facing one. Your hosting provider is unlikely to be able to modify this for you, especially if it's a shared hosting solution.

Yuck
  • 49,664
  • 13
  • 105
  • 135
  • Thanks, but that doesn't help at all. I need the fully qualified Url. That generates a Url that is relative, so in my second application the href tag that should point back to beta.sc-pa.com looks like this `href="/dr405//dr405/Wizard/Review/B00593.2660"` – Doug Chamberlain Jan 03 '12 at 14:05
  • The trouble you're going to have is that you need to interrogate the server running the code for the host name. But it (correctly) believes its host name is `SERVERNAME1.GROUP1.SUBGROUP.local`. Just because there is a public-facing DNS entry that routes traffic to it, doesn't mean it's aware of that. As I said, there could be **many** public-facing DNS entries that point to `SERVERNAME1.GROUP1.SUBGROUP.local`. – Yuck Jan 03 '12 at 14:08
0

If you're behind a load balancer or similar, it might be worth checking server variables. In our case we do something like this:

string hostName = Request.Headers["x-forwarded-host"];
hostName = string.IsNullOrEmpty(hostName) ? Request.Url.Host : hostName;

This question I asked a while ago might be of interest:

Asp.net mvc 301 redirect from www.domain.com to domain.com

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
0

Due to time constraints, and the need to get this project out the door, I had to resort to hardcoding the main part of the url into the application. After I made the change I felt stupid for trying to make it dynamic in the first place. I mean how often should our domain name change?

Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91