1

I get ReffererUrl from current User, if refferer is exists i need to extract hostname without .com/.co.uk .... etc. value from it. So if ReffererUrl is http://main.something.biz/sup.aspx?r=e3432r3 i want to get just "something".

Doesn't matter whether it is Regex or something else.

thanks...

eugeneK
  • 10,750
  • 19
  • 66
  • 101
  • 1
    in that case hostname part will be "main.something.biz". there's no way to get only "something" with the built-in functions. You can btw, extract the string yourself. – Arief Nov 11 '10 at 09:47
  • @Arief Iman Santoso, thanks.so i basically Remove from 0 to first dot and then from last dot till the end? Will it work in all cases? – eugeneK Nov 11 '10 at 09:49
  • 1
    no, it will be difficult. The hostname part can be in many form. "www.domain.com", "domain.com", "x.x.x.domain.com.xx", and so on.. – Arief Nov 11 '10 at 10:17

4 Answers4

2

Note: it is just for your specs only: you can extend it by adding more condition at the end of my code. but i'd say that it wont work when path is like "abc.ss33.video.somthing.co.us"

 Uri u = new Uri("http://main.something.biz/sup.aspx?r=e3432r3");
            string a = u.DnsSafeHost;
            string[] arr1 = a.Split('.');
            string somethinVar = String.Empty;

            if (arr1.Length == 3)
                somethinVar = arr1[1];
  • @Mazhar Karimi, thanks. I've made something similar just without array length check which is important in some cases. +1 for that. Now question is what happens when domain in fact is something like "abc.ss33.video.somthing.co.us" ? – eugeneK Nov 11 '10 at 10:10
  • @eugeneK, well its hard to get per your 'specific requirement', you would have to include all the domain extenstion in your logic, either you do it by string-functions or Regex. You can see for reference http://stackoverflow.com/questions/569137/how-to-get-domain-name-from-url –  Nov 11 '10 at 11:31
  • Another link: http://stackoverflow.com/questions/27745/getting-parts-of-a-url-regex –  Nov 11 '10 at 11:37
0

There is no built-in way to do this in the sense you describe, because neither IIS nor ASP.NET knows the difference between the host name and domain name.

You have to write some code to do that.

an example could be:

string hostName=ReffererUrl.split('.')[1];

This code works only if the ReffererUrl look like the one you have posted and you have to make sure the array that the split function return an array with a number of elements greater than 1

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0
HttpContext.Current.Request.ServerVariables("HTTP_HOST")
Ramiz Uddin
  • 4,249
  • 4
  • 40
  • 72
0

Extract domain with subdomain if present:-

Public Function ExtractSubAndMainDomainFromURL(URL As String) As String
    '
    ' cut-off any url encoded data
    URL = URL.Split("?"c)(0)
    'return array of segments between slashes
    Dim URLparts() As String = URL.Split("/"c)
    'find first segment with periods/full-stops
    Dim Domain As String = Array.Find(URLparts, Function(x) (x.Contains(".")))
    'check if nothing returned - if necessary
    If IsNothing(Domain) Then Domain = String.Empty 


    Return Domain

End Function
Andrew-UK
  • 1
  • 1
  • 2