0

OK, here's the thing... You fetch the user's IP address and based on his/her country you redirect the user to a specific webpage. Now, how do you change the master page dynamically? This is how I am redirecting the user :-

Geolocation error with IP address 127.0.0.1

It's not like the user clicks some button or something and you then change the master page. I want it changed when the user is redirected, so how exactly do I go about it?

public partial class testClass: System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (redirected to site1)
        {
              Use this master page1
        }
        else
            if (redirected to site2)
            {
                Use this master page2
            }
    }
}

So how do I check what SITE the user has been redirected to? Also HOW to apply the specific master page now that the user has been redirected?

I just need an idea how to go about it.

[EDIT] please check the code block below. How do I fetch the URL that the user has been redirected to? I actually need just the "iso3166TwoLetterCode" variable's value (see the link to my earlier question, please) and based on that will be changing the master page. I can't figure out how to fetch that value or even use that class (that's got this variable) in my testClass.

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (user Has been redirected to www.site.in )
        {
            this.MasterPageFile = "master1.master";
        }

        if (user Has been redirected to www.site.fr )
        {
            this.MasterPageFile = "master2.master";
        }
    }
Community
  • 1
  • 1
Serenity
  • 4,968
  • 19
  • 65
  • 104

2 Answers2

1

To find out the country two letter code, do what this sample code from http://ipaddressextensions.codeplex.com/ does:

using System.Net;
using WorldDomination.Net;

string userHostIpAddress = "203.1.2.3";
IPAddress ipAddress;
if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
{
    string country = ipAddress.Country(); // return value: UNITED STATES
    string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US
}

you can then try something like this to change the master:

protected void Page_PreInit(object sender, EventArgs e)
{
    this.MasterPageFile = "NewMasterSite.master";
}
Philipp
  • 11,549
  • 8
  • 66
  • 126
  • could you please check the additional details I just added?? thnx – Serenity Sep 11 '10 at 11:13
  • figured out how to fetch that code off the url..got help here http://stackoverflow.com/questions/3698196/how-do-i-check-for-a-urls-top-level-domain-in-asp-net-c – Serenity Sep 17 '10 at 07:10
0

It sounds like you're not redirecting to another site, just sending them to a page with a querystring like "language=en". If so, then you need to get it with Request.QueryString and append it to a base master page name.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
  • okk..so if it IS a webpage..how do I like fetch the URL (eg-www.mysite.in) and then check it using "Request.QueryString" ..after that how do I change the master page ? – Serenity Sep 11 '10 at 10:58
  • Based on your example, you're not using a querystring at all. Rather, you have multiple subdomains set up, probably on a virtual server. In that case, try `Request.Uri`. If that doesnt't work, `Request.ServerVariables["Host"]` will. – Steven Sudit Sep 11 '10 at 11:35
  • Allow me to correct myself: The HTTP header is called "Host", but you have to refer to it here through "HTTP_HOST". Alternately, I think "URL" will do what you want. – Steven Sudit Sep 11 '10 at 11:43