在没有会话的情况下访问asp.net中的公共类变量
我正在使用我发现的这个示例来学习如何加载类文件并通过它们访问变量。这是在 App_Code 文件夹中名为 Class1.vb 的文件中(这不是应用程序项目):
Imports Microsoft.VisualBasic
Public Class my_class
Public Shared Sub my_sub()
Dim vartest As String
vartest = 10
HttpContext.Current.Session("myvar") = vartest
End Sub
End Class
这是 aspx 文件上的代码隐藏:
Imports my_class
Partial Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
my_class.my_sub()
Label1.Text = HttpContext.Current.Session("myvar")
End Sub
End Class
How can I access the vartest variable without using a session,因为如果它被多个访问我假设变量可以被覆盖。是否可以采用另一种方式,将变量发送到类文件?
I am using this example I found to learn how to load class files and access variables through them. This is in a file called Class1.vb in the App_Code folder (this is not an app project):
Imports Microsoft.VisualBasic
Public Class my_class
Public Shared Sub my_sub()
Dim vartest As String
vartest = 10
HttpContext.Current.Session("myvar") = vartest
End Sub
End Class
This is the codebehind on the aspx file:
Imports my_class
Partial Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
my_class.my_sub()
Label1.Text = HttpContext.Current.Session("myvar")
End Sub
End Class
How could I access the vartest variable without using a session, since if this is accessed by multiple functions at the same time the variable can be overwritten I assume. Is it possible to go the other way, where a variable is sent to a class file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您需要快速了解一些基本的 ASP.Net Webforms 概念。首先,我将反驳一个常见的新手误解:
您的 Page 类不会在 Web 服务器上停留很
长时间用户会话访问其站点,并且每个回发或事件都使用相同的页面类实例。事实并非如此。 ASP.Net 页面类实例几乎总是在不到一秒的时间内创建和再次销毁,如果需要更长时间,大多数经验丰富的开发人员都会认为这是一个大问题。
ASP.NET 依赖于 HTTP 协议
这里要记住的是 ASP.Net 仍然依赖于 HTTP 协议,而 HTTP 可以归结为请求和响应。当您查看网页时,您的浏览器首先向服务器发送请求。服务器响应,通常使用 html 文档。然后浏览器会解析html;根据在 html 中看到的内容,浏览器可能会向服务器发送更多请求以获取其他资源,例如 javascript、图像或 css 文件。每个请求都会产生单独的响应,浏览器使用所有这些资源将页面呈现到屏幕上。然而,ASP.Net 运行时通常不必处理额外的请求(这会使速度变慢)——只有初始 html 需要 ASP.Net 支持;您希望其他资源是可以缓存的基本文件。
ASP.Net 运行时为每个请求创建一个新的类实例。
当 ASP.net 运行时处理页面请求时,它将创建页面类的新实例。运行时将遵循 ASP.Net 页面生命周期(这实际上应该是命名为“ASP.Net Page Request Lifecycle”),并按照生命周期定义的特定顺序调用此类实例中的某些方法或引发某些事件。
这意味着每个回发或事件都在您的类的不同实例中运行。
这也意味着每个回发或事件都会重建并传输所有进入您页面的 html,而不仅仅是您想要更改的部分。对于您的服务器代码,结果是类级变量在 ASP.Net 中唯一真正有用的东西是将在单个 http 请求中使用的东西。对于浏览器来说,结果是在每个事件之后你都在使用一个全新的 DOM。
要理解所有这些,重要的是要充分理解类和类实例之间的区别。您问题中的一些内容让我不确定您是否已经了解这一点。
ASP.Net 运行时在站点的所有用户之间共享一个应用程序实例
Web 服务器通常只为整个网站及其所有用户提供一个应用程序实例。因此,任何具有共享/静态范围的内容对于每个用户来说都是通用的。在 ASP.Net 中,共享/静态的内容很少是合适的。
那么,您如何处理应由单个用户或访问您的网站访问的数据?
这正是
会话
的用途。在任何给定时间,会话对于单个请求始终是唯一的。您担心多个函数同时访问会话,但这种情况不会发生。 ASP.Net 页面生命周期确保除非您手动生成额外的线程,否则对于给定的 HttpContext 和会话一次仅运行一个函数。如果用户以某种方式同时发送两个应该具有相同 Session/HttpContext 的请求,则一个请求将由 ASP.Net 运行时保留,直到另一个请求完成。如果您不想始终引用会话,则可以在类中构建包装会话变量的属性。有关示例,请参阅 @Pankaj 的回答。It sounds like you need a quick overview of some basic ASP.Net Webforms concepts. Up first I'll counter a common newbie misconception:
Your Page class does not hang around on the web server for very long
I think many new ASP.Net developers have this idea of the web server keeping a single instance of their page class for every user session that hits their site, and each postback or event uses this same page class instance. That's just not how it works. ASP.Net page class instances are nearly always created and destroyed again in well under a second, and most experienced developers see it as a big problem if it takes longer.
ASP.NET relies on the HTTP protocol
The thing to remember here is ASP.Net still relies on the HTTP protocol, and http boils down to requests and responses. When you view a web page, your browser first sends a request to a server. The server responds, usually with an html document. The browser will then parse the html; based on what it sees in the html the browser may send more requests to the server for additional resources, such as javascript, images, or css files. Each request results in a separate response, and the browser uses all these resources to render the page to the screen. However, the ASP.Net runtime normally does not have to process the additional requests (that would make things slower) — ony the initial html needs ASP.Net support; you want the other resources to be basic files that can be cached.
The ASP.Net runtime creates a new instance of your class for every request.
When the ASP.net runtime processes a request for a page, it will create a new instance of your page class. The runtime will follow the ASP.Net Page lifecycle (this should really be named the "ASP.Net Page Request Lifecycle"), and call certain methods or raise certain events in this class instance, in a specific order defined by the lifecycle.
This means every postback or event runs in a different instance of your class.
It also means every postback or event is rebuilding and transmitting all of the html the goes into your page, and not just the portions you want to change. For your server code, the consequence is the only thing class-level variables are really good for in ASP.Net is things that will be used within a single http request. For the browser, the consequence is you're working with a brand new DOM after every event.
To understand all of that, it's important here to also have a good understanding of the difference between a class and an instance of a class. A couple items in your question make me unsure whether you have this understanding yet.
The ASP.Net runtime shares one application instance among all users of your site
The web server typically only has one instance of your application for the entire web site and all it's users. Therefore, anything with a Shared/static scope is common to every user. It's rarely appropriate in ASP.Net for anything to be Shared/static.
So how do you handle data that should live with a single user or visit to your site?
This is exactly what the
Session
is for. A session will always be unique to an individual request at any given time. You're worried about multiple functions accessing the session at the same time, but this does not happen. The ASP.Net Page Lifecycle ensures that unless you manually spawn additional threads, only one function at a time is running for a given HttpContext and Session. If a user somehow sends two requests at about the same time that should have the same Session/HttpContext, one will be held by the ASP.Net runtime until the other is completed. If you don't want to reference the session all the time, you can build properties in your class that wrap session variables. See @Pankaj's answer for an example.首先,
Session
具有用户范围,因此它不会被另一个请求覆盖。是吗通过静态对象的静态属性访问asp.net会话变量是否安全?
您可以将访问封装到属性中:
然后您可以通过以下方式获取变量:
First, a
Session
has user-scope, so it will not be overwritten by another Request.Is it safe to access asp.net session variables through static properties of a static object?
You could encapsulate the access into a property:
Then you can get the variable by:
除了“Tim Schmelter”回复之外......
您还可以创建一个
BaseClass
,它将继承自“Tim”建议的 Place 属性。您需要做的唯一更改是将访问修饰符更改为
Protected
并且您应该删除Public and Shared
您还可以保留其他常见功能,我们可以在其中重用的属性其他类也...同样,您也可以为用户控件创建 BaseControls
最后,在 Web 表单中继承此类...
希望这会对您有所帮助...
基类代码
示例代码“Behind Code”-展示Protected的用法来自基类的成员数据
In addition to the "Tim Schmelter" reply....
You can create a
BaseClass
which will inherit fromPlace the property as suggested by "Tim". The only change you need to do is to change the access modifier to
Protected
and you should removePublic and Shared
You can also keep other common functions, properties that can we reused in other classes also... Similarly you can create BaseControls as well for your User controls
Finally, inherit this class in the web form....
Hope this will help you...
Base Class code
Sample Code "Behind Code" - Showing the usage of Protected member Data from Base Class
一般来说,您可以使用不同的位置来存储应用程序状态:应用程序(应用程序范围,将状态保存到应用程序域中),会话(可以保存当前浏览器会话将访问的所有内容),ViewState(存储在隐藏输入字段中的变量,并将在每次回发时发布)。当然你也可以将状态保存到数据库或文件中。我不确定你想要实现什么,但看起来你正在寻找像 ViewState 这样的东西。
阅读 ASP.NET 状态管理
Generally you can use different places to store application state: Application (application wide, saves state into application domain), Session (there can be saved everything what will be accessed by current browser session), ViewState (variables stored in hidden input field and will be posted on every postback). Of course you can also save state to database or file. I'm not sure what you want to achieve, but looks like you looking for something like ViewState.
Read ASP.NET State Management