global.asax 中的 application_start 和 session_start 事件中应放置哪些代码?
您能否举例说明最适合放置在 Application_Start 和 Session_Start 子例程中的内容? 我知道每个子例程何时被调用。 Application_Start 当第一个用户首次访问 Web 应用程序时。 Session_Start 当用户打开与应用程序的会话时。 但是这些子例程中的每一个都属于哪些代码。每个子程序中的代码应该做什么?
Can you give an example of what might be best suited to place in the Application_Start and Session_Start subroutines?
I know when each subroutine is called.
Application_Start when the first user first accesses the web application.
Session_Start when a user opens a session with the application.
But what code belongs in each of these subroutines. What should the code in each subroutine do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是有什么例子吗?那么,在 MVC 站点中,路由在 Application_Start 中注册。这也是初始化 IoC 容器(例如 StructureMap)的好地方。如果您希望应用程序中的一些单例在用户访问它们时易于使用而不是后期绑定(例如,如果它们的初始化成本很高,而您宁愿自己在网站上进行一次缓慢的初始点击),则可以初始化应用程序中的一些单例而不是打扰一两个顾客)。
Session_Start 通常使用较少,但可能是进行某种类型的每用户(或每会话,实际上)跟踪的好地方。
Just any examples? Well, in an MVC site the routes are registered in Application_Start. That's also a good place to initialize an IoC container such as StructureMap. Maybe initialize some singletons you have in your application if you want them to be readily available rather than late-bound when a user accesses them (like if they have a high initialization cost and you'd rather do one slow initial hit on the website yourself than bother a customer or two with it).
Session_Start is generally used less often, but could be a good place for per-user (or per-session, realistically) tracking of some kind.
Application_Start
通常用于初始化应用程序范围的设置,这些设置需要在每个应用程序域中完成一次,例如注册对象容器、读取一些配置初始化值……在Session_Start
中,您可以放置一些与启动会话的特定用户相关的代码。Application_Start
is often used to initialize application wide settings which need to be done once per application domain like registering object containers, reading some config initialization values, ... InSession_Start
you could place some code which is tied to the specific user who started the session.请小心 Application_Start 中的异常处理。在 IIS7 集成模式中,您不会有响应对象或 HTTPContext。请参阅此帖子:请求在此上下文中不可用
Be careful with exception handling in Application_Start. In IIS7 Integrated mode you won't have the response object or HTTPContext. See this thread: Request is not available in this context