[英]How does ASP.NET know to create the ASP.NET_SessionId cookie if Session_Start is defined?
I've done quite a bit of testing on this, and I'm thoroughly confused. 我已经做了很多测试,我很困惑。 It seems ASP.NET will generate an
ASP.NET_SessionId
cookie if the Session_Start
method in the MvcApplication
class is defined, even if I'm not using the Session
variable anywhere. 如果定义了
MvcApplication
类中的Session_Start
方法,ASP.NET似乎会生成一个ASP.NET_SessionId
cookie,即使我没有在任何地方使用Session
变量。 That seems odd, considering there doesn't have to be anything in the method's body. 考虑到方法体内没有任何东西,这看起来很奇怪。
Example (Global.asax.cs): 示例(Global.asax.cs):
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace MyApplication
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
private void Session_Start() { } // this is all it takes to generate a SessionId (?)
}
}
Now, I'm confused for multiple reasons: 现在,我因为多种原因感到困惑:
How is the mere presence of the Session_Start
function enough to generate a SessionId? 如何仅仅存在
Session_Start
函数来生成SessionId? I'm not utilizing the Session
variable anywhere in the application, and the method is empty. 我没有在应用程序的任何地方使用
Session
变量,并且该方法为空。
The Session_Start
method is private, and I'm obviously not calling it anywhere inside the class, so how does ASP.NET know when a session starts? Session_Start
方法是私有的,我显然不是在类中的任何地方调用它,所以ASP.NET如何知道会话何时启动?
How does anything outside of this class even know the Session_Start
method exists, and to check for a SessionId cookie? 本类之外的任何内容如何知道
Session_Start
方法是否存在,以及检查SessionId cookie? It isn't a partial class, and it's explicitly marked private
. 它不是部分类,它明确标记为
private
。
I know these reasons sort of blend into one another, but I'm really at a loss as to how this works. 我知道这些原因有点融合在一起,但我真的不知道它是如何运作的。
Session_Start
is like an event handler for the application. Session_Start
就像是应用程序的事件处理程序。 When a new session is created, this method is called by the application. 创建新会话时,应用程序将调用此方法。 It does not create Session IDs, it is meant as a way for the developer to know when a user visits the site for the first time (that session).
它不会创建会话ID,它意味着开发人员可以知道用户何时第一次访问该站点(该会话)。 This can be used to run some initialization routines or tracking purposes (eg, we had x number of unique sessions today).
这可以用于运行一些初始化例程或跟踪目的(例如,我们今天有x个唯一会话)。
What triggers the creation of the Session and SessionID is a user visiting a page that has session enabled-- ASP.NET will create the Session behind the scenes. 触发创建Session和SessionID的是用户访问已启用会话的页面 - ASP.NET将在后台创建Session。 The answer to this question has two ways of enabling session state for pages: Session state can only be used when enableSessionState is set to true either in a configuration
此问题的答案有两种启用页面会话状态的方法:只有在配置中将enableSessionState设置为true时才能使用会话状态
In summary: 综上所述:
in web.config, for all pages: 在web.config中,对于所有页面:
<system.web>
<pages enableSessionState="true" />
</system.web>
in your page.aspx, on a per-page basis (set it to false to turn it off on a per-page basis): 在page.aspx中,基于每页(将其设置为false以在每页基础上将其关闭):
<%@Page enableSessionState="true">
Your web.config should also configure the SessionState mode. 您的web.config还应配置SessionState模式。 This is an example of using the server's memory to store session state:
这是使用服务器的内存来存储会话状态的示例:
<sessionState cookieless="false" mode="InProc" timeout="20" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.