异常匹配在On Exception方法中发现错误 [英] Ambiguous match found error in On Exception method

查看:77
本文介绍了异常匹配在On Exception方法中发现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HandleError属性处理错误,并想知道异常发生在哪里我在OnException方法中做到这一点bur得到上面的错误因为我有两个同名的方法,一个是[HttpGet],另一个是[HttpPost],例如Create。但是我不能改变...所以如何获取动作名称和控制器名称?? plz帮助..



//代码



protected override void OnException(ExceptionContext filterContext)

{

  string  actionName = filterContext.RouteData.Values [  action]。ToString( ); 
类型controller = filterContext.Controller.GetType();
var method = controller.GetMethod(actionName); // 错误'找到了ambigous match'
var returnType = method.ReturnType;





}

解决方案

< blockquote>您可以更改代码并使用GetMethods()而不是GetMethod,通过actionName过滤结果,然后管理两种情况(对于两个属性:HttpGet和HttpPost)。


< b> GetMethods()返回一个 MethodInfo 类型的数组,因此你必须从这个数组中过滤掉项目,然后使用它们的属性(比如 item.ReturnType )。

i am handling error using HandleError attribute and want to know where exception occurs i am doing that in OnException method bur getting above error beacuse i have two methods with same name one is [HttpGet] and other is [HttpPost] eg Create..but that i cant change...so how to fetch action name and controller name ??plz help..

//code

protected override void OnException(ExceptionContext filterContext)
{

string actionName = filterContext.RouteData.Values["action"].ToString();
                Type controller = filterContext.Controller.GetType();
                var method = controller.GetMethod(actionName); //error 'ambigous match found'
                var returnType = method.ReturnType;



}

解决方案

You could change your code and use GetMethods() instead of GetMethod, filter the results by your actionName, and then manage both cases (for both attributes: HttpGet and HttpPost).


GetMethods() returns an array of type MethodInfo, so you have to filter the items from this array then to use the property of them (like item.ReturnType).


这篇关于异常匹配在On Exception方法中发现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆