【问题标题】:Communication layer between client and server客户端和服务器之间的通信层
【发布时间】:2012-11-21 02:07:41
【问题描述】:

我想知道是否有任何技术可以控制 Web 应用程序 (ASP.NET) 中客户端和服务器之间的通信

例子:

  • 请求数
  • 检查没有重复请求
  • 检查是否执行了操作

工作流程

  1. 客户端发送请求“A”
  2. 服务器收到请求“A”,并响应
  3. 服务器将请求“A”标记为已回答
  4. 客户端重新发送请求“A”
  5. 服务器回答请求“A”已得到响应

【问题讨论】:

  • With client...你的意思是网络浏览器吗?
  • 嗨阿德里亚诺!是的,我指的是浏览器:)
  • 你能详细说明你想要达到的目标吗?
  • 谢谢史蒂夫。我更新了问题。我添加了工作流(例如)

标签: c# asp.net client-server communication


【解决方案1】:

您可以在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
    }

【讨论】:

    【解决方案2】:

    在任何 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
            }
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 2014-08-04
      相关资源
      最近更新 更多