Created
November 30, 2011 23:58
-
-
Save willnss/1412052 to your computer and use it in GitHub Desktop.
Tempo de execução de uma Action/ASP.NET MVC 'manualmente'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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