本来是看MVC的filter的,不过看到例子里面有一个trace,就看看怎么用
先写个actionfilter
public class LoggingFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(FilterExecutingContext
filterContext)
{
filterContext.HttpContext.Trace.Write("Starting: " +
filterContext.ActionMethod.Name);
}
public override void OnActionExecuted(FilterExecutedContext
filterContext)
{
if (filterContext.Exception != null)
{
filterContext.HttpContext.Trace.Write("Exception thrown");
}
}
}
在controller中加入特性
public class HomeController : Controller
{
[LoggingFilter]
public void Index()
{
RenderView("Index");
}
[LoggingFilter]
public void About()
{
RenderView("About");
}
[LoggingFilter]
public void ClickMe()
{
HttpContext.Trace.Write("Button was clicked.");
InvokeAction("Index");
}
}
在webconfig中加入
<system.web>
<trace enabled="true" pageOutput="true" mostRecent="true"/>
</system.web>
运行程序后,分别点击controller对应的view,然后输入/Trace.axd,例如: