Click to See Complete Forum and Search --> : How to call an aspx page from a vb class?


elizab
May 23rd, 2005, 04:45 AM
Hi All,

How can I call an aspx page from a vb class. My vb project consists of only classes (forms are not present). I have given a reference to System.web. But I am not able to acces Response object or HttpContext.

Inside that class I need to use the response object for redirecting to an aspx page.
Can anyone help me.

Thanks in advance

Regards,
elizab

coolbiz
May 23rd, 2005, 07:18 AM
I don't think you can use Request/Response object directly from your VB.NET dll (unless passed in by an ASP.NET app).

What exactly that you want to do?

elizab
May 23rd, 2005, 09:08 AM
My requirement is to call an aspx page from a vb.net dll. so that i can reuse the same dll.
I want to redirect to the corresponding webpage from a dll.
Hope the requirement is clear.

Thanks for raising help
Regards,
elizab

coolbiz
May 24th, 2005, 07:17 AM
You can pass the Request and Response objects from the ASPX page into the DLL. Then your DLL can use those objects.

Public Class MyWebHelper
Private m_Server As HttpServerUtility = Nothing
Private m_Request As HttpRequest = Nothing
Private m_Response As HttpResponse = Nothing

Public Sub New(server As HttpServerUtility, request As HttpRequest, response As HttpResponse)
m_Server = server
m_Request = request
m_Response = response
End Sub

Public Sub RedirectToLogin()
m_Response.Redirect("mylogin.aspx")
End Sub
End Class

In ASPX class:

Private Sub Page_Load(...)
If (Not IsLoggedIn) Then
Dim hlpr As New MyWebHelper(Server, Request, Response)
hlpr.RedirectToLogin()
End
End Sub

Private Function IsLoggedIn() As Boolean
' validate user
End Function

subdigital
May 24th, 2005, 08:23 PM
no, that is completetly unnecessary.

If you can be sure that the code will be running withing a web request (ie, this class/method will not be used by any win forms code) then you can ALWAYS get access to the current request by doing this:

System.Web.HttpContext.Current

This will give you access to the Request/Response objects, as well as Session, Application, and cookies.

Hope this helps.

coolbiz
May 25th, 2005, 07:41 AM
Ha! Look at that ... learn something new every day.