简体   繁体   中英

Beginner ASP.NET C# question about dynamically changing master pages

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";
        }
    }

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";
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM