网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在ASP.NETCore中计算Http请求时间-创新互联

这篇文章给大家介绍怎么在ASP.NET Core中计算Http请求时间,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

在磴口等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、成都网站建设 网站设计制作按需求定制开发,公司网站建设,企业网站建设,品牌网站制作,成都全网营销,外贸营销网站建设,磴口网站建设费用合理。

ASP.NET 是什么

ASP.NET 是开源,跨平台,高性能,轻量级的 Web 应用构建框架,常用于通过 HTML、CSS、JavaScript 以及服务器脚本来构建网页和网站。

ASP.NET Core通过RequestDelegate这个委托类型来定义中间件

public delegate Task RequestDelegate(HttpContext context);

可将一个单独的请求委托并行指定为匿名方法(称为并行中间件),或在类中对其进行定义。可通过Use,或在Middleware类中配置要传递给委托执行的方法(参数类型HttpContext,返回值类型Task)。

public static IApplicationBuilder Use(this IApplicationBuilder app, Func, Task> middleware);

public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, params object[] args);

通过定义一个中间件类 来计算http请求的时间,例:

public class ResponseTimeMiddleware
{
  // Name of the Response Header, Custom Headers starts with "X-" 
  private const string RESPONSE_HEADER_RESPONSE_TIME = "X-Response-Time-ms";
  // Handle to the next Middleware in the pipeline 
  private readonly RequestDelegate _next;
  public ResponseTimeMiddleware(RequestDelegate next)
  {
    _next = next;
  }
  public Task InvokeAsync(HttpContext context)
  {
    // Start the Timer using Stopwatch 
    var watch = new Stopwatch();
    watch.Start();
    context.Response.OnStarting(() => {
      // Stop the timer information and calculate the time  
      watch.Stop();
      var responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
      // Add the Response time information in the Response headers.  
      context.Response.Headers[RESPONSE_HEADER_RESPONSE_TIME] = responseTimeForCompleteRequest.ToString();
      return Task.CompletedTask;
    });
    // Call the next delegate/middleware in the pipeline  
    return this._next(context);
  }
}

关于怎么在ASP.NET Core中计算Http请求时间就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章名称:怎么在ASP.NETCore中计算Http请求时间-创新互联
网站链接:http://bjjierui.cn/article/dhcoii.html

其他资讯