로깅 및 오류 처리를 위해 Log4net 및 HttpHandler를 사용하십시오. 다음 샘플 코드를 참조하십시오. 그리고 같은 ExceptionHandler
속성 컨트롤러를 장식합니다. ( [ExceptionHandler] public class BillsController : Controller { }
)
public class ExceptionHandler : HandleErrorAttribute
{
private static readonly ILog log = LogManager.GetLogger(typeof(ExceptionHandler));
public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
{
log.Debug("******** ExceptionHandler.OnException() *****************");
base.OnException(filterContext);
Exception ex = filterContext.Exception;
if (filterContext.Exception is BusinessException)
{
log.Debug("<<<<Inside On BusinessException....>>>>" + DateTime.Now);
//log.Debug("*** Error Getting From **** " + LOGIN_DETAILS.LoginUserName +
// "(UserId = " + LOGIN_DETAILS.LoginUserID + ")" + "Time =" + DateTime.Now);
BusinessException _BusinessException =
(BusinessException)filterContext.Exception;
StringBuilder errormsg = new StringBuilder();
foreach(MessageInfo msg in _BusinessException.ErrorMessageList)
{
errormsg.AppendLine(msg.LocalizedMessage);
}
log.Error("<<<<--------BusinessException-------->>>> : Exception Details -----"+ errormsg.ToString());
//filterContext.ExceptionHandled = true;
filterContext.ExceptionHandled = true;
RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Account", action = "Logout", message = filterContext.Exception.Message })).VirtualPath;
filterContext.HttpContext.Response.Redirect(url, true);
}
else
{
log.Error("Exception Details ---- " +
"MESSAGE: " + ex.Message +
"\nSOURCE: " + ex.Source +
"\\Controller: " + filterContext.Controller.ToString() +
"\nTARGETSITE: " + ex.TargetSite +
"\nSTACKTRACE: " + ex.StackTrace,
ex);
filterContext.ExceptionHandled = true;
RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Exception", action = "Default" })).VirtualPath;
//string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "exception", action = "Default", message = filterContext.Exception.Message })).VirtualPath;
filterContext.HttpContext.Response.Redirect(url, true);
}
}
}
감사 amesh. .HttpContext.Response.Redirect (url, true) 중에 컨트롤러 액션에 모델을 전달할 수있는 방법이 있습니까? – daniele
물론 .. RedirectToAction ("Action", "Controller"new {model = yourmodel})을 시도해 볼 수 있습니다. (새로운 질문은 다른 질문으로 권장됩니다) – amesh