Request.Url.Host

Last post 05-16-2008 10:46 AM by Jasbits. 2 replies.

Sort Posts:

  • Request.Url.Host

    05-16-2008, 9:14 AM
    • Member
      25 point Member
    • Jasbits
    • Member since 10-23-2005, 3:14 PM
    • Posts 147

    Hi

     I need to make class so I can use Request.Url.Host anywhere in the site. but I do something wrong, the class doesen't return the value to the caling page

    Imports Microsoft.VisualBasic
    Public Class IncomingUrlClass
    nherits System.Web.UI.Page

    Private _IncomingURL As String

    Public Property IncomingURL() As String
    Get
    Return _IncomingURL
    End Get
    Set(ByVal value As String)
    _IncomingURL = Request.Url.Host
    End Set
    End Property
    End Class

    Calling page:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then

    Dim IncomingUrl As IncomingUrlClass = New IncomingUrlClass
    Dim CompanyLogo As New IncomingCompany()

    Dim CompanyLogos As CommonDAL.IncomingCompanyDataTable = CompanyLogo.SelectIncomingByURL(IncomingUrl.IncomingURL)
    Dim Image As CommonDAL.IncomingCompanyRow = CompanyLogos(0)

    Logo.ImageUrl = "~/PageImages/" + Image.IncomingLogo
    End If

    End Sub

     

    Thanks

  • Re: Request.Url.Host

    05-16-2008, 9:40 AM
    Answer
    • All-Star
      28,218 point All-Star
    • johram
    • Member since 06-13-2006, 10:36 AM
    • Sweden
    • Posts 3,543
    • Moderator

    The root of your problem seems to be that you never initialize the _IncomingURL member, unless you explicitly set it. I think you're on the wrong track here. Make a constructor instead which reads out the Url property and remove the Set accessor.

    Another problem is that your IncomingURL class inherits from Page. While it isn't really a Page. But I assume that you've done so because otherwise you could not reach the Request object, am I right? The good news is that you can actually access the Request object - without being inside a Page. You do this by using the HttpContext object.

    Also the naming of your class is somewhat strange. Typically you should refrain from using the term "Class" in your class name. So a more appropriate name would be "IncomingUrl". But that's just a recommendation - and it's not what's causing the problem.

    Finally, it's a bit misleading that your class suggests that it handles the Url when you actually return the host part of the Url. Therefore, I think you should make one property to return the Url and one for the Host. Here's my suggested implementation:

    Imports Microsoft.VisualBasic

    Public Class IncomingUrl
    Private _IncomingURL As String

    Public Sub New
    (ByVal value As Integer)
    _IncomingURL = HttpContext.Request.Url
    End Sub

    Public Property
    URL() As String
    Get
    Return
    _IncomingURL.ToString()
    End Get
    End Property

    Public Property
    Host() As String
    Get
    Return
    _IncomingURL.Host
    End Get
    End Property

    End Class
    Actually, after having rewritten the code, I realize this entire class is totally redundant. You'll do much better if you access HttpContext.Request.Url.Host directly Wink
    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Request.Url.Host

    05-16-2008, 10:46 AM
    • Member
      25 point Member
    • Jasbits
    • Member since 10-23-2005, 3:14 PM
    • Posts 147

    Hi Thanks for youre quick replay.

    I have adapted some of youre input

    Imports Microsoft.VisualBasic

    Public Class IncomingUrlClass

    Private _IncomingURL As String

    Public Sub New()

    _IncomingURL = HttpContext.Current.Request.Url.Host

    End Sub

    Public Property IncomingURL() As String

    Get

    Return _IncomingURL

    End Get

    Set(ByVal value As String)

    _IncomingURL = value

    End Set

    End Property

    End Class

Page 1 of 1 (3 items)