问题描述:

After many many years of using Classic ASP, I am attempting to delve into the world of ASP.Net using VB.

I have gotten way to use to being able to declare variables on a page, inlclude that file and use it everywhere.

I am struggling to declare a global variable. I just need

Dim TheUser As String = Request.ServerVariables("AUTH_USER")

Then use it on a page as <%=TheUser%>

I have tried

Module GlobalVars

Dim TheUser As String = Request.ServerVariables("AUTH_USER")

End Module

But then I get "'Request' is not declared."

If I use Dim TheUser As String = Request.ServerVariables("AUTH_USER")

within a Page_Load sub, the Request part is fine, but then I cant use it on my .aspx page. I get 'TheUser' is not declared. It may be inaccessible due to its protection level.

I apologize in advance as if this an extremely remedial question.

I just dont know where I am suppose to declare it, or maybe you can't declare variables to be used everywhere.

网友答案:

You want:

Module GlobalVars
     Dim TheUser As String = Httpcontext.Current.Request.ServerVariables("AUTH_USER")
End Module
网友答案:

Welcome to .Net!

I would suggest that you abandon the Classic ASP way of doing things, and delve into more more web architecture and practices.

With that being said, look at Forms Authentication to start. You will have access to objects that will have the values you are looking to use globally.

Obviously there are many ways to do things, and this is just one.

相关阅读:
Top