Re: request, session and application 'not defined'?
- From: Misbah Arefin <MisbahArefin@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 13 Jan 2008 15:23:02 -0800
James says he gets the message that "Request" object in undefined not the
ServerVariables object... also he says that he gets this error in some class
he is making which is probably NOT derived from System.Web.Page
the solution is to use "HttpContext.Current" property to get the Request or
Session object
The static property Current on the HttpContext class can be useful whenever
the flow of control leaves the code in your Page derived web form. Using this
property you can reach out and magically grab the current Request, Response,
Session, and Application objects (and more) for the request you are servicing
--
Misbah Arefin
"Juan T. Llibre" wrote:
re:.
!> You do have a reference to System.Web, right?
That is not necessary.
System.Web is a built-in imported class and doesn't need a reference.
You don't need Imports Microsoft.VisualBasic nor Imports System.Data, either.
The code works fine as is...
See it working at : http://asp.net.do/test/path_info.aspx
The problem is that you can't get the ServerVariables in a "regular" sub.
You must get the ServerVariables in Page_Load...which is why you're getting the error.
Here's the code for that sample page :
-------------------------------------------------------
<%@ Page Language="VB" %>
<html>
<head>
<title>Path Info</title>
</head>
<script runat="server">
Public Sub Page_Load(Sender As System.Object, E As System.EventArgs)
path.Text = "Request.ServerVariables('Path_Info') = " & Request.ServerVariables("Path_Info")
Dim strPathURL as string
strPathURL = Left(Request.ServerVariables("Path_Info"), InStrRev(Request.ServerVariables("Path_Info"), "/"))
path2.Text = "Left(Request.ServerVariables('Path_Info'), InStrRev(Request.ServerVariables('Path_Info'), '/')) = " &
strPathURL
End Sub
</script>
<html>
<body>
<form id="Form1" runat="server">
<p>
<asp:Label id="path" runat="server" /><BR>
<asp:Label id="path2" runat="server" /><BR>
</form>
</body>
</html>
--------------
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Scott M." <smar@xxxxxxxxxxxxx> wrote in message news:%23G77oMjVIHA.6044@xxxxxxxxxxxxxxxxxxxxxxx
You do have a reference to System.Web, right?
"James R. Davis" <Jim@xxxxxxxxxxx> wrote in message news:%23p6hoohVIHA.6044@xxxxxxxxxxxxxxxxxxxxxxx
I did not have System.Web imported but when I added it there was no change
whatever in the VS diagnostic.
Further, when I tried changing the Request to HTTPRequest the diagnostic
changed to:
"Reference to a non-shared member requires an object reference"
All I am trying to do is convert some ASP code to ASP.NET as a learning
strategy. Your help is greatly appreciated.
"Scott M." <smar@xxxxxxxxxxxxx> wrote in message
news:udL0pVhVIHA.5360@xxxxxxxxxxxxxxxxxxxxxxx
The actual classes are: HTTPRequest, HTTPServer and HTTPSession. But, youyour
can use Request, Server and Session if you have System.Web imported in
code. Do you?went
"James R. Davis" <Jim@xxxxxxxxxxx> wrote in message
news:u0qfFThVIHA.5208@xxxxxxxxxxxxxxxxxxxxxxx
I am building a set of shared functions and subroutines in a vb file
located
in my App_Code section of my application. The first three functions
fine and work as expected. But I am now starting a subroutine that must
work with certain server variables and I have been stopped cold.
This is the code that frustrates me:
Sub GetConfig()
Dim strPathURL as string
strPathURL = Left (Request.ServerVariables("Path_Info"),
InStrRev(Request.ServerVariables("Path_Info"), "/"))
...
end Sub
Visual Studio states that [Name 'Request' is not declared.]
Indeed, it does the same for any statement I try that uses 'Request',
'Server', 'Session' or 'Application'.
I started the VB page with:
Imports Microsoft.VisualBasic
Imports System.Data
What more do I need to do? Thanks!!
- Follow-Ups:
- Re: request, session and application 'not defined'?
- From: Juan T. Llibre
- Re: request, session and application 'not defined'?
- References:
- request, session and application 'not defined'?
- From: James R. Davis
- Re: request, session and application 'not defined'?
- From: Scott M.
- Re: request, session and application 'not defined'?
- From: James R. Davis
- Re: request, session and application 'not defined'?
- From: Scott M.
- Re: request, session and application 'not defined'?
- From: Juan T. Llibre
- request, session and application 'not defined'?
- Prev by Date: Re: 2005 to 2008
- Next by Date: Re: request, session and application 'not defined'?
- Previous by thread: Re: request, session and application 'not defined'?
- Next by thread: Re: request, session and application 'not defined'?
- Index(es):
Relevant Pages
|