tags:

views:

146

answers:

4

Let's say "www.mysite.fr/home" is the URL ..Now how do I fetch "fr" out of this ? JUST "fr"

actually what I am trying to do is change masterpages at runtime after detecting a visitor's country..Yes I can use the countryCode variable which is there in some other class but thot may be I can do it like this only..just wanted to try..logic basically is:-

if(its "fr")
{
//apply masterpage 1
}


if(its "in")
{
//apply masterpage 2
}

Is it possible ? What will be the right way anyway ? Making that class that contains CountryCode variable as a utility class and using the variable from there in my new class

OR

fetch this value "fr" or "in" off the URL ?? How do I do this ? Is it possible?

+1  A: 

I think this should be possible. You can use regex to get the code (fr, in etc.) and change the master page but you'll have to do this before the page_load. By the time asp.net reaches page_load the master page is already set (if I remember correctly). You'll need to handle the PreInit event and set the master page you want to set. So basically do all the regex and master page changing in the PreInit event and you're good to go :)

Here's a description of PreInit (Source: http://msdn.microsoft.com/en-us/library/ms178472.aspx):

PreInit Raised after the start stage is complete and before the initialization stage begins.

Use this event for the following:

•Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.

•Create or re-create dynamic controls.

•Set a master page dynamically.

•Set the Theme property dynamically.

•Read or set profile property values.

Sidharth Panwar
yeah..have included this logic inside of Page_PreInit only coz MasterPageFile doesn't work in Page load..if I am not wrong...so regex..huh? lemme google..thnx
Serenity
If your URL remains the same, like www.mysite.fr, www.mysite.in, www.mysite.us, then you can use simple string handling but for complex expressions you might need regex. Its the granddaddy of string matching :)
Sidharth Panwar
+5  A: 
 if (Request.Url.Host.ToLower().EndsWith(".fr"))
  {
    ...
  }
Mark Cidade
I am a bit confused abt what "Request.Url.Host" returns...if my URL is www.mysite.fr/home/myAccount.aspx...is it going to return "www.mysite.fr" OR the complete URL ie "www.mysite.fr/home/myAccount.aspx" ???
Serenity
It will just return "www.mysite.fr"
Mark Cidade
@Mark - I think you need to make the url `ToUpper()` before checking..
Danny Chen
oh cool..if this is gonna return just that..I think this is the way to go..this way I don't have to worry about substrings in the URL ..am I correct ??
Serenity
I added ToLower(). Thanks : )
Mark Cidade
@happysoul correct
Mark Cidade
@Mark.. my logic is inside of Page_PreInit event of a MASTER.cs file..is that ok or do I need to include this logic inside of some class or something? sry if it sounds dumb
Serenity
As long as you have access to the Request object, you should be fine. If you get a "Request is not defined" error, then use "HttpContext.Current.Request"
Mark Cidade
ohk..thanks....
Serenity
+3  A: 

There is no method to be used directly. Maybe you can write an extension yourself:

public static string GetDomainTypeName(this Uri uri)
{
  if (!uri.HostNameType.Equals(UriHostNameType.Dns) || uri.IsLoopback)
     return string.Empty; // or throw an exception
  return uri.Host.Split('.').Last();
}

And be careful about the word case! WWW.GOOGLE.FR may make your code incorrect.

Danny Chen
A: 

For the Current Scenario you can try with the following snippet:

string url = "www.mysite.fr/home";
            int nStrLength = url.Length;
            int nDot = url.LastIndexOf(".")+1;

            int nRestStringLngth = nStrLength - nDot;

            string baseDomain = url.Substring(nDot, nRestStringLngth);
            int nSlash = baseDomain.IndexOf("/");
            baseDomain = baseDomain.Substring(0, nSlash);
            Console.WriteLine(baseDomain);
Subhen