2010-08-04 57 views
6

在asmx web服務中的C#中,如何獲取調用web服務的當前域? HttpContext.Current.Request.Url.Host返回kindof我想要的,而不是http://mydomain.com/Folder/Mywebservice.asmx我只需要http://mydomain.com。 我知道我可以切斷那串,但它看起來真的很優雅。 感謝獲取ASMX中的當前域

+0

下面的鏈接可能重複 - http://stackoverflow.com/questions/61817/whats-the-best-method-in-asp-net-to-obtain-the-current-domain你會在這裏得到答案。 – 2010-08-04 09:42:21

回答

6

Uri.GetLeftPart在這裏幫助:

Request.Url.GetLeftPart(UriPartial.Authority) 
2

在VB.Net我用...

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

或者,如果你所關心的端口,然後...

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

應該很容易轉換爲C#;)