gpt4 book ai didi

asp.net-core - 使用 ASP.NET Core 中间件时如何处理 global.asax 文件中的所有事件

转载 作者:行者123 更新时间:2023-12-03 07:33:35 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Migrating global.asax to ASP.NET 5

(3 个回答)


5年前关闭。




这是 global.asax 文件中的主要事件列表。假设当我们为 global.asax 文件中的每个事件编写代码来处理情况以及何时使用 ASP.NET Core 中间件时,然后告诉我如何处理 ASP.NET Core 中间件中的每个事件代码。请提供一个示例代码来解决 ASP.NET Core 中间件中的所有事件。

应用程序_初始化:

Fired when an application initializes or is first called. It is invoked for all HttpApplication object instances.



Application_Disposed:

Fired just before an application is destroyed. This is the ideal location for cleaning up previously used resources.



应用程序_错误:

Fired when an unhandled exception is encountered within the application.



应用程序_开始:

Fired when the first instance of the HttpApplication class is created. It allows you to create objects that are accessible by all HttpApplication instances.



申请_结束:

Fired when the last instance of an HttpApplication class is destroyed. It is fired only once during an application's lifetime.



Application_BeginRequest:

Fired when an application request is received. It is the first event fired for a request, which is often a page request (URL) that a user enters.



Application_EndRequest:

The last event fired for an application request.



Application_PreRequestHandlerExecute:

Fired before the ASP.NET page framework begins executing an event handler like a page or Web service.



Application_PostRequestHandlerExecute:

Fired when the ASP.NET page framework has finished executing an event handler.



Applcation_PreSendRequestHeaders:

Fired before the ASP.NET page framework sends HTTP headers to a requesting client (browser).



Application_PreSendContent:

Fired before the ASP.NET page framework send content to a requesting client (browser).



Application_AcquireRequestState:

Fired when the ASP.NET page framework gets the current state (Session state) related to the current request.



Application_ReleaseRequestState:

Fired when the ASP.NET page framework completes execution of all event handlers. This results in all state modules to save their current state data.



Application_ResolveRequestCache:

Fired when the ASP.NET page framework completes an authorization request. It allows caching modules to serve the request from the cache, thus bypassing handler execution.



Application_UpdateRequestCache:

Fired when the ASP.NET page framework completes handler execution to allow caching modules to store responses to be used to handle subsequent requests.



Application_AuthenticateRequest:

Fired when the security module has established the current user's identity as valid. At this point, the user's credentials have been validated.



Application_AuthorizeRequest:

Fired when the security module has verified that a user can access resources.



session 开始:

Fired when a new user visits the application Web site.



session 结束:

Fired when a user's session times out, ends, or they leave the application Web site.



谢谢

最佳答案

关于 ASP.NET Core 中的 session

经典 ASP.NET 包括几个与 session 相关的事件:Session_Start 和 Session_End,您可以通过 global.asax 访问它们以执行代码。在 ASP.NET Core 1.0 中,您可以使用中间件查询 session 集合,以确定是否已建立 session 以复制 Session_Start 事件,但没有计划引入与 Session_End 等效的功能。

由于 ASP.NET Core 1.0 背后的驱动力之一是“云就绪”,因此 session 管理设计的重点一直是使其在分布式场景中工作。 Session_End 仅在 session 使用 inproc 模式(本地服务器内存)和 .NET Core 团队时才会触发 have stated that they won't add features that only work locally .

看看这里管理Application Session in ASP.NET Core .

HttpModules

HttpModules 已被 ASP.NET Core 中的中间件取代,Application_BeginRequest 或 Application_EndRequest 等事件是 global.asax 的方法,它是一个 Http_Module。
所以,要替换那些你必须编写自己的中间件。

我不会为您编写该代码,但您需要的一切(带有代码示例!)都在这里:Migrating HTTP handlers and modules to ASP.NET Core middleware .

认证

关于Application_AuthenticateRequest,您还应该阅读有关Request features的文档.

关于asp.net-core - 使用 ASP.NET Core 中间件时如何处理 global.asax 文件中的所有事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41159087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com