HighTechTalks DotNet Forums  

Searching URL String for Subdomain value

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Searching URL String for Subdomain value in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
CSINVA
 
Posts: n/a

Default Searching URL String for Subdomain value - 10-31-2007 , 09:29 PM






I have my DNS setup to answer for any subdomain. I need to be able to
look at the URL typed in like "joe.mysite.com" and put the subdomain
in a variable, so I can then look it up in a database. This would have
to work if they typed in http://joe.mysite.com or just joe.mysite.com.

Here is what I have started with, but as you can see I'm a little
stuck.

Sub ExtractDomain(ByVal expr As String)

Dim sURL As String = Page.Request.Url.ToString()
Dim subDomain = InStr(1, sURL, "http://")
If subDomain > 0 Then
'True so count over 7 characters


Else
'False, so start immediately


End If

End Sub


Reply With Quote
  #2  
Old   
Tim Mackey
 
Posts: n/a

Default Re: Searching URL String for Subdomain value - 11-12-2007 , 05:39 AM






hi,
you should use regular expressions for this job, they are in their element
here.
you can use the expression [\w-]+ to match all the components of a URL.
for example, running Regex.Matches on http://joe.mysite.com will yield 4
captures in the first group:
http joe mysite com
you can then decide how you want to deal with them. you want to support
someone using HTTPS, and also a domain without a subdomain, such as
http://mysite.com or even http://mysite if running in an intranet
environment.
plenty of regex tutorials on the net and in the SDK docs if you need a
refresher on regex.
good luck
tim

"CSINVA" <mcse_instructor (AT) yahoo (DOT) com> wrote

Quote:
I have my DNS setup to answer for any subdomain. I need to be able to
look at the URL typed in like "joe.mysite.com" and put the subdomain
in a variable, so I can then look it up in a database. This would have
to work if they typed in http://joe.mysite.com or just joe.mysite.com.

Here is what I have started with, but as you can see I'm a little
stuck.

Sub ExtractDomain(ByVal expr As String)

Dim sURL As String = Page.Request.Url.ToString()
Dim subDomain = InStr(1, sURL, "http://")
If subDomain > 0 Then
'True so count over 7 characters


Else
'False, so start immediately


End If

End Sub



Reply With Quote
  #3  
Old   
Ben Voigt [C++ MVP]
 
Posts: n/a

Default Re: Searching URL String for Subdomain value - 11-12-2007 , 11:34 AM




"CSINVA" <mcse_instructor (AT) yahoo (DOT) com> wrote

Quote:
I have my DNS setup to answer for any subdomain. I need to be able to
look at the URL typed in like "joe.mysite.com" and put the subdomain
in a variable, so I can then look it up in a database. This would have
to work if they typed in http://joe.mysite.com or just joe.mysite.com.

Use System.Uri and the TryCreate method, then check the Host property.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.