본문 바로가기

.Net

ASP.NET MVC BaseController 내 동작

- BaseController

namespace MyFramework.Web.MyService.Controllers {
     public class BaseController : System.Web.Mvc.Controller
     {
         public ViewDataModel model;
          public BaseController()
         {
             model = new ViewDataModel();
         }
          protected override void Execute(System.Web.Routing.RequestContext requestContext)
         {
             MyFramework.Common.Logging.CommonLog.Error("Execute start");
             base.Execute(requestContext);
             MyFramework.Common.Logging.CommonLog.Error("Execute end");
         }
          /// <summary> 작업 메서드가 호출되기 전에 호출됩니다.
         /// </summary>
         /// <param name="filterContext">현재 요청 및 작업에 대한 정보입니다.</param>
         protected override void OnActionExecuting(ActionExecutingContext filterContext)
         {
             MyFramework.Common.Logging.CommonLog.Error("OnActionExecuting start");
             base.OnActionExecuting(filterContext);
             MyFramework.Common.Logging.CommonLog.Error("OnActionExecuting end");
         }
          /// <summary> 작업메서드가 호출된후 호출됩니다.
         /// </summary>
         /// <param name="filterContext">현재 요청 및 작업에 대한 정보입니다.</param>
         protected override void OnActionExecuted(ActionExecutedContext filterContext)
         {
             MyFramework.Common.Logging.CommonLog.Error("OnActionExecuted start");
             base.OnActionExecuted(filterContext);
             MyFramework.Common.Logging.CommonLog.Error("OnActionExecuted end");
         }
          /// <summary> 작업 메서드에서 반환되는 작업 결과가 실행된 후에 호출됩니다.
         /// </summary>
         /// <param name="filterContext">현재 요청 및 작업 결과에 대한 정보입니다.</param>
         protected override void OnResultExecuted(ResultExecutedContext filterContext)
         {
             MyFramework.Common.Logging.CommonLog.Error("OnResultExecuted start");
             base.OnResultExecuted(filterContext);
             MyFramework.Common.Logging.CommonLog.Error("OnResultExecuted end");
         }
          /// <summary> 작업 메서드에서 반환되는 작업 결과가 실행되기 전에 호출됩니다.
         /// </summary>
         /// <param name="filterContext">현재 요청 및 작업 결과에 대한 정보입니다.</param>
         protected override void OnResultExecuting(ResultExecutingContext filterContext)
         {
             MyFramework.Common.Logging.CommonLog.Error("OnResultExecuting start");
             base.OnResultExecuting(filterContext);
             MyFramework.Common.Logging.CommonLog.Error("OnResultExecuting end");
         }
          /// <summary> 작업에서 처리되지 않은 예외가 발생할 때 호출됩니다.
         /// </summary>
         /// <param name="filterContext">현재 요청 및 작업에 대한 정보입니다.</param>
         protected override void OnException(ExceptionContext filterContext)
         {
             MyFramework.Common.Logging.CommonLog.Error("OnException start");
             string errorMessage = "\r\n\t RawUrl[" + filterContext.HttpContext.Request.RawUrl + "]" +
                                    "\r\n\t UrlReferrer[" + filterContext.HttpContext.Request.UrlReferrer + "]";
             
             MyFramework.Common.Logging.CommonLog.Error(errorMessage, filterContext.Exception);
             this.View("Index").ExecuteResult(this.ControllerContext);
  
             filterContext.ExceptionHandled = true;
             filterContext.HttpContext.Response.Clear();
             MyFramework.Common.Logging.CommonLog.Error("OnException end");
         }
          /// <summary> 요청이 이 컨트롤러와 일치하지만 지정된 작업 이름을 포함하는 메서드를 컨트롤러에서 찾을 수 없을 때 호출됩니다.
         /// 각 컨트롤러 마다 index Action을 지정하여 해당 페이지로 이동하게 한다.
         /// </summary>
         /// <param name="actionName">시도된 작업의 이름입니다.</param>
         protected override void HandleUnknownAction(string actionName)
         {
             MyFramework.Common.Logging.CommonLog.Error("HandleUnknownAction : acionName[" + actionName + "]");
             this.View("Index").ExecuteResult(this.ControllerContext);
         }

 

- HomeController

namespace MyFramework.Web.MyService.Controllers
 {
     public class HomeController : BaseController
     {
         public ActionResult Index()
         {
             MyFramework.Common.Logging.CommonLog.Error("Controller start");
             ShopBiz shopBiz = new ShopBiz();
             int pageNo = 1;
             if (Request["no"] != null)
                 Int32.TryParse(Request["no"].ToString(), out pageNo);
             ViewData["ShopList"] = shopBiz.ReadTShopInfoList(pageNo, 10);
             MyFramework.Common.Logging.CommonLog.Error("Controller end");
             return View(model);
         }
          public ActionResult About()
         {
             return View(model);
         }
          public ActionResult Test()
         {
             return View(model);
         }
     }
 }

 

Logging 실행결과
- 정상실행시
ERROR log_common (null) - Execute start
ERROR log_common (null) - OnActionExecuting start
ERROR log_common (null) - OnActionExecuting end
ERROR log_common (null) - Controller start
ERROR log_common (null) - Controller end
ERROR log_common (null) - OnActionExecuted start
ERROR log_common (null) - OnActionExecuted end
ERROR log_common (null) - OnResultExecuting start
ERROR log_common (null) - OnResultExecuting end
ERROR log_common (null) - OnResultExecuted start
ERROR log_common (null) - OnResultExecuted end
ERROR log_common (null) - Execute end

- 에러발생유도시

ERROR log_common (null) - Execute start
ERROR log_common (null) - OnActionExecuting start
ERROR log_common (null) - OnActionExecuting end
ERROR log_common (null) - Controller start
ERROR log_common (null) - OnActionExecuted start
ERROR log_common (null) - OnActionExecuted end
ERROR log_common (null) - OnException start
ERROR log_common (null) -
  RawUrl[/home/index?no=-12]
  UrlReferrer[]
/**
Exception Error message........
**/
ERROR log_common (null) - OnException end
ERROR log_common (null) - Execute end

- 잘못된 ActionName 입력시

ERROR log_common (null) - Execute start
ERROR log_common (null) - HandleUnknownAction : acionName[index1]
ERROR log_common (null) - Execute end