本文整理汇总了C#中System.Web.Mvc.ExceptionContext类的典型用法代码示例。如果您正苦于以下问题:C# ExceptionContext类的具体用法?C# ExceptionContext怎么用?C# ExceptionContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ExceptionContext类属于System.Web.Mvc命名空间,在下文中一共展示了ExceptionContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnException
public override void OnException(ExceptionContext context)
{
base.OnException(context);
if (!context.ExceptionHandled)
LogException(context.Exception);
}
示例2: getStatusCodeHelper
//Helpers--------------------------------------------------------------------------------------------------------------//
#region Helpers
//getStatusCodeHelper
private HttpStatusCode getStatusCodeHelper(ExceptionContext exceptionContext)
{
var exceptionType = exceptionContext.Exception.GetBaseException().GetType();
if (exceptionType == typeof(ArgumentNullException)) { return HttpStatusCode.BadRequest; }
if (exceptionType == typeof(DbBadRequestException)) { return HttpStatusCode.BadRequest; }
return HttpStatusCode.InternalServerError;
}
示例3: Abnormal
/// <summary>
/// 处理异常,将异常保存到数据库
/// </summary>
/// <param name="filterContext"></param>
public void Abnormal(ExceptionContext filterContext)
{
MODEL.T_Abnormal abnormal = new MODEL.T_Abnormal();
string stack = filterContext.Exception.StackTrace;
string[] str = stack.Split('.');
string area = str[0];
string controller = str[1];
string action = str[2];
string[] str1 = action.Split('(');
string reallyaction = str1[0];
abnormal.Area = area;
abnormal.Controller = controller;
abnormal.ACtion = reallyaction;
abnormal.Message = filterContext.Exception.Message;
OperateContext.Current.BLLSession.IAbnormalBLL.Add(abnormal);
//接下来在配置文件设置重定向
//注意:customErrors要放在system.web下
//string filePath = Server.MapPath("~/ExcelModel/Exception.txt");
//FileInfo file = new FileInfo(filePath);
//if (!file.Exists)
//{
// file.Create().Close();
//}
//StreamWriter sw = System.IO.File.AppendText(filePath);
}
示例4: OnException
protected override void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
string exceptionPolicy = this.ExceptionPolicyName;
if (string.IsNullOrEmpty(exceptionPolicy))
{
exceptionPolicy = DefautExceptionPolicyName;
}
try
{
if (ExceptionPolicy.HandleException(filterContext.Exception, exceptionPolicy))
{
base.OnException(filterContext);
}
else
{
this.ModelState.AddModelError(Guid.NewGuid().ToString(), filterContext.Exception);
filterContext.Result = this.View();
}
}
catch (Exception ex)
{
filterContext.Exception = ex;
base.OnException(filterContext);
}
}
示例5: OnException
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled) return;
if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500) return;
if (!ExceptionType.IsInstanceOfType(filterContext.Exception)) return;
string controllerName = filterContext.GetController();
string actionName = filterContext.GetAction();
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model)
};
//使用log4net写入本地日志
_logger.Error(filterContext.Exception.Message, filterContext.Exception);
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.ExceptionHandled = true;
}
示例6: OnException
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Controller is ServiceController)
{
ServiceController controller = (ServiceController)filterContext.Controller;
object model;
int httpStatusCode = (int)HttpStatusCode.InternalServerError;
IServiceException serviceException = filterContext.Exception as IServiceException;
if (serviceException!=null)
{
model = serviceException.Model;
httpStatusCode = (int)serviceException.StatusCode;
}
else
{
model = ExceptionToErrorModel(filterContext.Exception);
}
// Sort the accept types acceptable to the client into order of preference then look for
// a response handler that supports one of the accept types.
foreach (var contentTypeWrapper in ActionInvoker.GetAcceptHeaderContentTypes(controller.RequestInfo.AcceptTypes))
{
var replacementResult = NServiceMVC.Formatter.CreateContentResult(contentTypeWrapper.ContentType.ToString(), model);
if (replacementResult != null)
{
filterContext.Result = new HttpStatusContentResult(httpStatusCode, replacementResult);
filterContext.ExceptionHandled = true;
return;
}
}
}
}
示例7: OnException
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
{
return;
}
if (ExceptionType.IsInstanceOfType(filterContext.Exception) == false)
{
return;
}
ActionResult result;
if (filterContext.Exception.GetType() == typeof(ApplicationException))
{
Console.ReadLine();
//result = GetCustomException(filterContext);
//filterContext.HttpContext.Response.StatusCode = 400;
//var id = GenerateNewGuid();
//LogException(filterContext.Exception, id);
}
else
{
}
}
示例8: OnException
/// <summary>
/// Called when an exception occurs.
/// </summary>
/// <param name="filterContext">The filter context.</param>
public override void OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.Exception != null)
{
//Call exception handler for logging the exception
ExceptionManager.HandleException(filterContext.Exception);
// Handling HTTP & Ajax requests.
filterContext.ExceptionHandled = true;
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
// storing errormessage and stacktrace
SessionStateManager<ErrorState>.Data.ErrorMessage = filterContext.Exception.Message;
SessionStateManager<ErrorState>.Data.StackTrace = filterContext.Exception.StackTrace;
// preparing redirect url
Controller controller = (Controller)filterContext.Controller;
string redirectUrl = controller.Url.SmartAction(ErrorControllerAction, ErrorController);
filterContext.Result = new RedirectResult(redirectUrl);
}
else
{
filterContext.Result = OperationResult<bool>.CreateErrorResult(
filterContext.Exception.Message,
filterContext.Exception.StackTrace).ToJsonResult();
}
}
}
示例9: OnException
public void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled == true)
{
HttpException httpExp = filterContext.Exception as HttpException;
if (httpExp.GetHttpCode() != 500)
return;
}
HttpException httpException = filterContext.Exception as HttpException;
if (httpException != null)
{
filterContext.Controller.ViewBag.UrlRefer = filterContext.HttpContext.Request.UrlReferrer;
filterContext.HttpContext.Session["CurrentException"] = httpException;
if (httpException.GetHttpCode() == 404)
{
filterContext.HttpContext.Response.Redirect("~/Login/NotFound");
}
else if (httpException.GetHttpCode() == 500)
{
filterContext.HttpContext.Response.Redirect("~/Login/InternalError");
//filterContext.Result = new ViewResult() { ViewName = "InternalError", ViewData = filterContext.Controller.ViewData };
//filterContext.Result=new RedirectToRouteResult(("Default", new RouteValueDictionary(new { controller = "Login", action = "InternalError",route}));
}
//else if (httpException.GetHttpCode() == 900)
//{
// filterContext.HttpContext.Response.Redirect("~/Login/SignIn");
//}
}
//record log in file
filterContext.ExceptionHandled = true;//indicate the exp is handled
}
示例10: OnException
public void OnException(ExceptionContext filterContext)
{
Exception exception = filterContext.Exception;
string message;
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
message = "Ajax访问时引发异常:";
AjaxResult ajaxResult = null;
if (exception is BusinessException)
{
message = exception.Message;
ajaxResult = new AjaxResult(message, AjaxResultType.Warning);
}
else if (exception is HttpAntiForgeryException)
{
message += "安全性验证失败。<br>请刷新页面重试,详情请查看系统日志。";
}
else
{
message += exception.Message;
}
filterContext.Result = new JsonResult() { Data = ajaxResult ?? new AjaxResult(message, AjaxResultType.Error) };
filterContext.ExceptionHandled = true;
}
else
{
filterContext.Result = new ContentResult() { Content = "系统异常:" + exception.Message };
}
}
示例11: OnException
protected override void OnException(ExceptionContext filterContext)
{
try
{
StoreFrontConfiguration storeFrontConfig = GStoreDb.GetCurrentStoreFrontConfig(Request, true, false);
if (storeFrontConfig == null)
{
AddUserMessage("Store Front Inactive or not found!", "Sorry, this URL does not point to an active store front. Please contact us for assistance.", AppHtmlHelpers.UserMessageType.Danger);
filterContext.ExceptionHandled = true;
RedirectToAction("Index", "Home", new { area = "" }).ExecuteResult(this.ControllerContext);
}
}
catch (Exceptions.StoreFrontInactiveException)
{
AddUserMessage("Store Front Inactive!", "Sorry, this URL points to an Inactive Store Front. Please contact us for assistance.", AppHtmlHelpers.UserMessageType.Danger);
filterContext.ExceptionHandled = true;
RedirectToAction("Index", "Home", new { area = "" }).ExecuteResult(this.ControllerContext);
}
catch (Exceptions.NoMatchingBindingException)
{
AddUserMessage("Store Front Not Found!", "Sorry, this URL does not point to an active store front. Please contact us for assistance.", AppHtmlHelpers.UserMessageType.Danger);
filterContext.ExceptionHandled = true;
RedirectToAction("Index", "Home", new { area = "" }).ExecuteResult(this.ControllerContext);
}
catch (Exception)
{
throw;
}
base.OnException(filterContext);
}
示例12: LogExceptionDetails
private void LogExceptionDetails(ExceptionContext exceptionContext)
{
var builder = new StringBuilder();
builder.Append("Unhandled exception from ");
builder.AppendFormat(" {0}", exceptionContext.HttpContext.Request.UserHostAddress);
string forwardedFor = exceptionContext.HttpContext.Request.Headers.Get("X-Forwarded-For");
if (!string.IsNullOrWhiteSpace(forwardedFor))
{
builder.AppendFormat(" ({0})", forwardedFor);
}
builder.AppendFormat(" to: {0}", exceptionContext.HttpContext.Request.RawUrl);
builder.AppendFormat(
" - {0}/{1} {2}",
exceptionContext.Controller.ControllerContext.RouteData.Values["controller"],
exceptionContext.Controller.ControllerContext.RouteData.Values["action"],
exceptionContext.HttpContext.Request.HttpMethod);
builder.Append(Environment.NewLine);
builder.Append("-------------");
builder.Append(Environment.NewLine);
builder.Append(exceptionContext.Exception);
_log.Error(builder.ToString(), exceptionContext.Exception);
}
示例13: OnException
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext == null) return;
//记录异常信息
ApplicationContext.ApplicationLog.Log(LoggerLevels.Error, filterContext.Exception);
base.OnException(filterContext);
}
示例14: OnException
public void OnException(ExceptionContext filterContext)
{
if (!(filterContext.Exception is HttpAntiForgeryException))
return;
filterContext.Result = new RedirectResult("/");
filterContext.ExceptionHandled = true;
}
示例15: OnException
protected override void OnException(ExceptionContext filterContext)
{
//Exception exception = filterContext.Exception;
//string message;
//if (filterContext.HttpContext.Request.IsAjaxRequest())
//{
// message = "Ajax访问时引发异常:";
// AjaxResult ajaxResult = null;
// if (exception is BusinessException)
// {
// message = exception.Message;
// ajaxResult = new AjaxResult(message, AjaxResultType.Warning);
// }
// else if (exception is HttpAntiForgeryException)
// {
// message += "安全性验证失败。<br>请刷新页面重试,详情请查看系统日志。";
// }
// else
// {
// message += exception.Message;
// }
// filterContext.Result = Json(ajaxResult??new AjaxResult(message, AjaxResultType.Error));
// filterContext.ExceptionHandled = true;
//}
//else
//{
// filterContext.Result =Content("系统异常:"+exception.Message);
//}
}