Skip to content

Instantly share code, notes, and snippets.

@willnss
Created November 30, 2011 23:58
Show Gist options
  • Save willnss/1412052 to your computer and use it in GitHub Desktop.
Save willnss/1412052 to your computer and use it in GitHub Desktop.
Tempo de execução de uma Action/ASP.NET MVC 'manualmente'
//TesteApp\TimeExecutionAttribute.cs
using System.Diagnostics;
namespace TesteApp
{
public class ExecutionTimeAttribute : ActionFilterAttribute
{
private Stopwatch _stopwatch;
public ExecutionTimeAttribute()
{
_stopwatch = new Stopwatch();
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_stopwatch.Start();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
_stopwatch.Stop();
filterContext.Controller.ViewData["ExecutionTime"] = _stopwatch.Elapsed.ToString();
}
}
}
//TesteApp\Controllers\HomeController.cs
namespace TesteApp.Controllers
{
[ExecutionTime]
public class HomeController : Controller
{
public ActionResult Index()
{
//processamento
return View();
}
}
}
//TesteApp\Views\Home\Index.aspx
<h2>Teste</h2>
<p>
Tempo gasto no processamento <%:ViewData["ExecutionTime"]%>...
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment