Asp.net mvc 获取操作局部变量名称和值OneException自定义筛选器
要将操作的变量值检索到自定义筛选器中吗Asp.net mvc 获取操作局部变量名称和值OneException自定义筛选器,asp.net-mvc,asp.net-mvc-filters,Asp.net Mvc,Asp.net Mvc Filters,要将操作的变量值检索到自定义筛选器中吗 public class TrackError : IExceptionFilter { public void OnException(ExceptionContext filterContext) { // How to get the value of X ?????????????????
public class TrackError : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// How to get the value of X ?????????????????
}
}
控制器:
[TrackError]
public class HomeController : Controller
{
public ActionResult Index()
{
int x = 0;
throw new Exception("XYZ");
return View();
}
}
创建自定义异常并将所需的任何附加数据放入其属性中。然后在异常过滤器中的一个catch块中捕获该异常类型
public class ServiceException : Exception, ISerializable
{
public WhateverType X {get;set;}
public string Message{get;set;}
public ServiceException()
{
// Add implementation.
}
public ServiceException(WhateverType x, string message)
{
this.X = x;
this.Message = message;
}
public ServiceException(string message):base(message)
{
}
public ServiceException(string message, Exception inner)
{
// Add implementation.
}
// This constructor is needed for serialization.
protected ServiceException(SerializationInfo info, StreamingContext context)
{
// Add implementation.
}
}
然后在过滤器中:
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is ServiceException)
{
//Here you can access (context.Exception as ServiceException).X
}
}
抛出异常,如:
throw new ServiceException(X, "Your custom message gore here");
创建自定义异常并将所需的任何附加数据放入其属性中。然后在异常过滤器中的一个catch块中捕获该异常类型
public class ServiceException : Exception, ISerializable
{
public WhateverType X {get;set;}
public string Message{get;set;}
public ServiceException()
{
// Add implementation.
}
public ServiceException(WhateverType x, string message)
{
this.X = x;
this.Message = message;
}
public ServiceException(string message):base(message)
{
}
public ServiceException(string message, Exception inner)
{
// Add implementation.
}
// This constructor is needed for serialization.
protected ServiceException(SerializationInfo info, StreamingContext context)
{
// Add implementation.
}
}
然后在过滤器中:
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is ServiceException)
{
//Here you can access (context.Exception as ServiceException).X
}
}
抛出异常,如:
throw new ServiceException(X, "Your custom message gore here");
你试过这种方法吗
int x = 0;
try
{
DoSomethingThatMightFail(s);
}
catch (Exception ex) when (Log(ex, "An error occurred", new[]{x,s}))
{
// this catch block will never be reached
}
...
static bool Log(Exception ex, string message, params object[] args)
{
Debug.Print(message, args);
return false;
}
你试过这种方法吗
int x = 0;
try
{
DoSomethingThatMightFail(s);
}
catch (Exception ex) when (Log(ex, "An error occurred", new[]{x,s}))
{
// this catch block will never be reached
}
...
static bool Log(Exception ex, string message, params object[] args)
{
Debug.Print(message, args);
return false;
}
我仍然无法在筛选器------context.Exception.X中获取变量值,因为系统给了我一个错误。Exception不包含您必须将异常强制转换为的XY的定义(context.Exception as ServiceException).X请告诉我在异常筛选器中检索操作的局部变量的其他方法,或者给我一些要研究的链接。谢谢,阿加尼,别用别的方法。这正是自定义异常和异常筛选器的用途。但我无法获得确切的异常消息,上下文仅显示一种类型的消息。异常。消息=>引发了类型为“MVCDemo.Common.ServiceException”的异常。但我仍然无法获取筛选器中的变量值------Context.Exception.X给了我一个错误,该错误是System.Exception不包含XY的定义。您必须将异常强制转换为(context.Exception作为ServiceException).X请告诉我在异常筛选器中检索操作的局部变量的其他方法,或给我一些要研究的链接。谢谢,againi从未使用任何其他方法。这正是自定义异常和异常筛选器的用途。但我无法获得确切的异常消息,上下文仅显示一种类型的消息。Exception.message=>Exception在上引发了类型为“MVCDemo.Common.ServiceException”的。