General.Web/Filters/ResponseTimeFilterAttribute.cs
(完整代码) 1KBusing Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace General.Mvc.Filters
{
public class ResponseTimeFilterAttribute : ActionFilterAttribute
{
private const string ResponseTimeKey = "ResponseTimeKey";
public override void OnActionExecuting(ActionExecutingContext context)
{
// Start the timer
context.HttpContext.Items[ResponseTimeKey] = Stopwatch.StartNew();
}
public override void OnActionExecuted(ActionExecutedContext context)
{
Stopwatch stopwatch = (Stopwatch)context.HttpContext.Items[ResponseTimeKey];
// Calculate the time elapsed
var timeElapsed = stopwatch.Elapsed;
ContentResult result = new ContentResult();
result.Content = "web service started, process time :" + timeElapsed.ToString();
context.Result = result;
}
}
}
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
