简体   繁体   中英

How do I check for a URL's top-level domain in ASP.NET/C#?

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?

 if (Request.Url.Host.ToLower().EndsWith(".fr"))
  {
    ...
  }

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.

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.

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);

In france it work with EndsWith .fr but in England you have .co.uk or in austria you have .co.at and also .at

You can use the following nuget package. (Install-Package Nager.PublicSuffix) https://www.nuget.org/packages/Nager.PublicSuffix/

Example:

 var domainParser = new DomainParser();
 var data = await domainParser.LoadDataAsync();
 var tldRules = domainParser.ParseRules(data);
 domainParser.AddRules(tldRules);

 var domainName = domainParser.Get("sub.test.co.uk");
 //domainName.Domain = "test";
 //domainName.Hostname = "sub.test.co.uk";
 //domainName.RegistrableDomain = "test.co.uk";
 //domainName.SubDomain = "sub";
 //domainName.TLD = "co.uk";

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