针对 C# Web 表单上的每个请求运行的事件

Event that runs on every request on C# web forms

我需要 运行 秒之前或在服务器上发出的每个请求时发生的事件。

如果该事件存在,我需要 运行 关于该事件的一些代码。

例如:有人输入 http://www.website.com/some-link

我需要先检查这个 link 在它被应用程序处理之前。

我正在使用 C# 网络表单。

到目前为止,我正在尝试在 Global.asax.cs 中查找事件,例如

protected virtual void Application_BeginRequest(object sender, EventArgs e)
{
    // need to track requested url
    // code that needs to be run with requested url.
}

这种情况有什么好的解决办法吗?

Tnx

正确的解决方案是使用事件 Application_BeginRequest,除非你想让事情复杂化并使用我不推荐的 httpmodules。

protected virtual void Application_BeginRequest(object sender, EventArgs e)
{
    var url = Request.Url.AbsoluteUri;
    var path = Request.Url.AbsolutePath;
    var host = Request.Url.Host;

    // your magic goes here
}

正如一些信息,所有事件的顺序是:

  • Application_BeginRequest
  • Application_AuthenticateRequest
  • Application_AuthorizeRequest
  • Application_ResolveRequestCache
  • Application_AcquireRequestState
  • Application_PreRequestHandlerExecute
  • Application_PreSendRequestHeaders
  • Application_PreSendRequestContent
    • asp.net页面代码执行完毕
  • Application_PostRequestHandlerExecute
  • Application_ReleaseRequestState
  • Application_UpdateRequestCache
  • Application_EndRequest