Page URL

G

Guest

I've created a custom base class for my asp.net web pages that needs to
retrieve the URL of the calling page from the New method. Problem is that
the Request property of the page is not available until after the base code
completes. How can I get the page URL from the New method?

Partial Class MyPage
Inherits MyBaseClass
End Class

Public Class MyBaseClass
Inherits System.Web.UI.Page

Public Sub New()
'Need to get page URL here, but Request is not available yet
Dim url as string = Request.ServerVariables("HTTP_HOST")

'do stuff based on url
End Sub
End Class
 
G

Guest

Hi there,

First, you can call base class constructor by MyBase.New(). second it
doesn't matter because Request property is instaintiated after construction
and the first event/method that HttpRequest class has bee instaintiated is
preinit:

Partial Class MyPage
Inherits MyBaseClass
End Class

Public Class MyBaseClass
Inherits System.Web.UI.Page

Public Sub New()
MyBase.New()
End Sub

Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs)
MyBase.OnPreInit(e)

' instead of using servervariables collection for url retrival
' it's easier to use Url property of the current request
Dim url As String = Request.Url.Authority

End Sub

End Class

Third, you don't need to access servervariables direclty as everything is
encapsulated in an instance of the httprequest class.

hope this helps
 
G

Guest

Perfect! Thanks Milosz.

Milosz Skalecki said:
Hi there,

First, you can call base class constructor by MyBase.New(). second it
doesn't matter because Request property is instaintiated after construction
and the first event/method that HttpRequest class has bee instaintiated is
preinit:

Partial Class MyPage
Inherits MyBaseClass
End Class

Public Class MyBaseClass
Inherits System.Web.UI.Page

Public Sub New()
MyBase.New()
End Sub

Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs)
MyBase.OnPreInit(e)

' instead of using servervariables collection for url retrival
' it's easier to use Url property of the current request
Dim url As String = Request.Url.Authority

End Sub

End Class

Third, you don't need to access servervariables direclty as everything is
encapsulated in an instance of the httprequest class.

hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top