访问在asp.net公共类变量,而会议 [英] Accessing public class variables in asp.net without session

查看:106
本文介绍了访问在asp.net公共类变量,而会议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个例子,我发现学习如何通过它们装入类文件和访问的变量。这是一个在APP_ code文件夹,名为将Class1.vb文件(这是不是一个应用程序项目):

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

这是对aspx文件的codebehind:

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

我怎样才能访问vartest变量不使用会话,因为如果这是由多种功能同时变量可以覆盖我假定访问。是否有可能走另外一条路,其中一个变量被发送到一个类文件?

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?

推荐答案

这听起来像你需要一些基本的ASP.Net Web表单概念的简要概述。截至第一,我会反击一个共同的误解新手:

It sounds like you need a quick overview of some basic ASP.Net Webforms concepts. Up first I'll counter a common newbie misconception:

我想了很多新的ASP.Net开发者有这种想法的Web服务器保持自己的网页类的单个实例击中他们的网站上每个用户,每个回发或事件是使用相同的页面类实例。这只是不是它是如何工作。几乎总是在ASP.Net页面类实例和摧毁再次远低于一秒钟,大多数开发商会认为这是一个大问题,当他们需要更长的时间。

I think a lot of new ASP.Net developers have this idea of the web server keeping a single instance of their page class for every user that hits their site, and each postback or event is using that 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 one second, and most developers would see it as a big problem when they take longer.

这里要记住的一点是,asp.net仍然依赖于HTTP协议和HTTP归结为请求响应。当您查看一个网页,什么情况是,您的浏览器首先发送的要求的到服务器。服务器的响应的,平时用的HTML文档。那么浏览器就会解析HTML;根据它所认为的HTML浏览器可能会发送更多的请求的到服务器进行额外的资源,如JavaScript,图片或CSS文件。浏览器将使用所有这些资源的页面呈现在屏幕上。然而,asp.net运行时一般不会有处理其他请求(这将让事情更慢)— ONY需要最初的HTML。

The thing to remember here is that asp.net still relies on the http protocol, and http boils down to requests and responses. When you view a web page, what happens is that 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. The browser will use all of 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 is needed.

当ASP.net运行时处理一个页面的请求时,它会创建页面类的新实例。运行时将遵循 ASP.Net页面生命周期(这应该被命名为ASP。网页的请求的生命周期),并调用某些方法或提高某些事件在这个类的实例,通过生命周期定义的顺序。

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.

这意味着每次回发或事件类的不同实例中运行。

这也意味着,每一个回发或事件被重建和发送的 HTML中的所有进入你的页面,你想改变不只是部分。为您的服务器code,其后果是,唯一的类级变量是在ASP.Net的东西,将一个HTTP请求中使用真正的好。对于浏览器,其后果是,你在每一个事件之后,一个全新的DOM的

It also means that every postback or event is rebuilding and transmitting all of the html the goes into your page, and not just the portions you wish to change. For your server code, the consequence is that 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 that you're working with a brand new DOM after every event.

要了解这一切,这一点很重要在这里,你也有一类和类的实例之间的差别有很好的理解。有你的问题了几个项目,使我不能确定你有认识呢。

To understand all of that, it's important here that you also have a good understanding of the difference between a class and an instance of a class. There are a couple items in your question that make me unsure you have that understanding yet.

Web服务器将只需要您的应用程序对整个网站的一个实例,它所有的用户。这意味着,一个共享/静态作用域什么是共同的每一个用户。这是很少适合在ASP.Net任何东西保存到共享的数据/静态的。

The web server will only have one instance of your application for the entire web site and all it's users. That means that anything with a Shared/static scope is common to every user. It's rarely appropriate in ASP.Net for anything that holds data to be Shared/static.

这是<强>究竟的会话是什么。会话将始终是唯一的在任何给定时间一个单独的请求。你担心多种功能的同时访问会话,但这种情况不会发生。在ASP.Net页面生命周期保证,除非你手动产卵额外的线程,只有一次一个功能正在运行一个给定的HttpContext和会话。如果用户以某种方式将在大约同一时间两个请求应该有他们的会话/ HttpContext的,一会由ASP.Net运行时举行,直到其他已完成。如果你不想引用会话的时候,你可以在你的类包装会话变量生成属性。请参见 @的Pankaj的一个示例答案

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 them 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.

这篇关于访问在asp.net公共类变量,而会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆