[LogActionFilter]
public class MyController : Controller
{
public ActionResult Index()
{
TempData["MyModel"] = model as MyModel;
//
}
public ActionResult Index2()
{
TempData["MyModel"] = model as MyModel;
//
}
}
public class LogActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Controller.TempData["MyModel"] != null)
{
MyModel model = (MyModel)filterContext.Controller.TempData["MyModel"];
try
{
Helper.SendEmail(jobReview);
}
catch (Exception)
{
// notify action result method on MyController that exception is happened
}
}
}
}
如何在控制器的Index或Index2动作结果方法中通知调用者代码引发一些异常?ActionFilter应该通知控制器的调用者代码抛出异常
你抽象掉用'LogActionFilter'记录方面(这是明智的)。但是,如果日志记录过程中出现问题,并且希望通知“主要”代码,则表明这可能不是真正的不同方面,并且应该成为控制器的一部分。无论如何,你必须认识到''OnActionExecuted''在调用action方法后被调用*。在这个阶段,它只是你的视图(或其他一些'ActionResult')可以通知的。 – haim770