4

I get url as

http://orders.mealsandyou.com/default.php

i dont want to use string functions to use it to get the main domain ie

mealsandyou.com

is there any function in c# to do that, UrilAuthority and all gives subdomain too...

Suggestions welcome, not workarounds

1Mayur
  • 3,419
  • 5
  • 40
  • 64
  • 1
    "i dont want to use string functions" - why? – Mitch Wheat Sep 04 '12 at 08:54
  • @MitchWheat I prefer not to use workarounds/kludge. .NET is very powerful framework, it should/must have something for this too – 1Mayur Sep 04 '12 at 08:56
  • If there's is this kind of function in .NET libraries they will still return domains as string. I cant understand what do you mean in: "i dont want to use string functions to use it to get the main domail" – Leri Sep 04 '12 at 08:57
  • since when is parsing using string functions a kludge? – Mitch Wheat Sep 04 '12 at 08:57
  • I don't think you can do this in a general fashion as its different for different tlds, eg mealsandyou.com and mealsandyou.co.uk. – Jon Egerton Sep 04 '12 at 08:57
  • @MitchWheat dnt take me as a wrong, that will be the last thing I want to use, I prefer it that way, you are taking me wrong – 1Mayur Sep 04 '12 at 08:58
  • 2
    I assume what he's looking for is a uri.subdomain and uri.domain on the URI object. (except it isn't there!) – Jon Egerton Sep 04 '12 at 08:58
  • The only constant part of the domain string is the TLD. technically everything else is a subdomain, so its subjective as to which part is the bit you're insterested as being the domain name rather than the subdomain. – Jon Egerton Sep 04 '12 at 09:00
  • @MitchWheat is its nothing defined in library, we have to use string then.. but I'm looking for more options to achieve this(if any) – 1Mayur Sep 04 '12 at 09:00
  • @JonEgerton, what I'm trying to do is if this subdomain is entered in the browser without any querystring then it should redirect it to TLD – 1Mayur Sep 04 '12 at 09:02
  • 2
    But the TLD is `com`, which is probably not what you want, you want one below the TLD. But with `mealsandyou.co.uk` you probably want 2 below the TLD. While it was once considered bad practice to have a 2-letter or less domain for anything other than a NIC, such sensible policies have been frowned on for some time, so `u.tv` and `o2.ie` are examples of how you can't use that as a guide. – Jon Hanna Sep 04 '12 at 09:10
  • 1
    possible duplicate of [Extract domain name from URL in C#](http://stackoverflow.com/questions/4120793/extract-domain-name-from-url-in-c-sharp) – daryal Sep 04 '12 at 09:36
  • @daryal its not duplicate, I want it from subdomain. and if subdomain is not present then it will just give that only – 1Mayur Sep 04 '12 at 09:39
  • @JonEgerton please post your ans, I will select it as the right ans – 1Mayur Sep 04 '12 at 09:47

2 Answers2

3

.Net doesn't provide a built-in feature to extract specific parts from Uri.Host. You will have to use string manipulation or a regular expression yourself.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
1

The only constant part of the domain string is the TLD. The TLD is the very last bit of the domain string, eg .com, .net, .uk etc. Everything else under that depends on the particular TLD for its position (so you can't assume the next to last part is the "domain name" as, for .co.uk it would be .co.

In any case I think you're taking the wrong approach. URL rewriting is far more suited to this sort of thing. Have a read of this: learn.iis.net/page.aspx/460/using-the-url-rewrite-module

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129