小编Gua*_*Wan的帖子

asp.net mvc Type.GetMethod当两个动作同名时,但获取和发布

在asp.net mvc 4中,我编写了一个自定义的异常处理程序,作为名为HandleCustomError的属性.

在这个处理程序中,我应该知道当前请求的动作,然后我就可以知道这个动作的返回类型是什么.我将返回"return type view()和json"的不同数据.

但是,现在我有两个同名的动作,但一个是"Get",另一个是"Post".方法"GetMethod"返回错误:"System.Reflection.AmbiguousMatchException"

public class HandleCustomError : System.Web.Mvc.HandleErrorAttribute
{
    public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
    {
        //base.OnException(filterContext);
        if (filterContext.ExceptionHandled)
        {
            return;
        }
        else
        {
            //Determine the return type of the action
            string actionName = filterContext.RouteData.Values["action"].ToString();
            Type controllerType = filterContext.Controller.GetType();
            var method = controllerType.GetMethod(actionName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            var returnType = method.ReturnType;
        }
        ....(Omitted)
Run Code Online (Sandbox Code Playgroud)

post get asp.net-mvc-4

7
推荐指数
1
解决办法
1171
查看次数

标签 统计

asp.net-mvc-4 ×1

get ×1

post ×1