7

In C# in an asmx web service how do I get the current domain that the webservice was called on? HttpContext.Current.Request.Url.Host returns kindof what I want but instead of http://mydomain.com/Folder/Mywebservice.asmx I just need http://mydomain.com. I know i could just cut that string up but it seems really in-elegant. Thanks

Matt
  • 1,562
  • 2
  • 17
  • 27
  • Possible duplicate of the link below - http://stackoverflow.com/questions/61817/whats-the-best-method-in-asp-net-to-obtain-the-current-domain You will get answer here. – Sachin Shanbhag Aug 04 '10 at 09:42

2 Answers2

7

Uri.GetLeftPart helps here:

Request.Url.GetLeftPart(UriPartial.Authority)
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
3

In VB.Net I have used...

With HttpContext.Current.Request.Url
    sDomain=.Scheme & System.Uri.SchemeDelimiter & .Host
End With

Or if you care about the Port then...

With HttpContext.Current.Request.Url
    sDomain=.Scheme & System.Uri.SchemeDelimiter & .Host & IIf(.IsDefaultPort,"",":") & .Port
End With

Should be easy to convert to C# ;)

barrylloyd
  • 1,599
  • 1
  • 11
  • 18