客户端和服务器之间的通信层

3
我想知道是否有任何技术可以控制Web应用程序(ASP.NET)中客户端和服务器之间的通信,例如:
  • 请求数量
  • 检查不重复请求
  • 检查操作是否已执行
工作流程如下:
  1. 客户端发送请求“A”
  2. 服务器接收到请求“A”,并回复
  3. 服务器将请求“A”标记为已回答
  4. 客户端重新发送请求“A”
  5. 服务器回答请求“A”已被回答

1
你说的_client_是指网页浏览器吗? - Adriano Repetti
嗨,Adriano!是的,我指的是浏览器 :) - Alejandro Mosquera
你能详细说明一下你想要实现什么吗? - Steve B
谢谢 Steve。我更新了问题。我添加了工作流程(作为示例)。 - Alejandro Mosquera
2个回答

2
您可以在Global.asax文件中使用以下方法拦截请求:
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var request = ((System.Web.HttpApplication)(sender)).Context.Request;
        //here you can evaluate and take decisions about the request
    }

0
在任何ASP.NET应用程序中,您都可以使用HttpApplication事件跟踪所需的更改。例如,您可以使用BeginRequest和/或EndRequest事件进行跟踪:
protected void Application_BeginRequest(object sender, EventArgs e)
{
    if(MyGlobalFlags.TrackingRequests){
        //  do stuff
    }
}

protected void Application_EndRequest(object sender, EventArgs e)
{
    if(MyGlobalFlags.TrackingRequests){
        //  do stuff
    }
}

个人意见,我会使用一个全局标志,如果需要的话可以轻松关闭它。

如果您正在谈论ASP.NET MVC应用程序,则建议在要跟踪的操作中使用ActionFilters。您可以实现自己的ActionFilter类,并在OnActionExecuted和/或OnResultExecuted上跟踪这些更改。我仍然会使用全局标志来关闭跟踪而不更改代码。

public class MyTrackingActionFilter: ActionFilterAttribute{
    public override OnActionExecuted(ActionExecutedContext filterContext)
    {
           if(MyGlobalFlags.TrackingRequests){
            //  do stuff
        }
    }

    public override OnResultExecuted(ActionExecutedContext filterContext)
    {
           if(MyGlobalFlags.TrackingRequests){
            //  do stuff
        }
    }
}

作为一条注释,我不建议在这些事件中尝试进行重型操作。如果轨道需要进行可以并行运行的大量数据库操作,我建议您在使用线程池的同时使用队列系统。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接