代码改变世界

MVC5 您不能调用控制器“xx”上的操作方法“xx”,因为该方法是一种泛型方法

2017-07-13 08:56  Dorisoy  阅读(1127)  评论(0编辑  收藏  举报

在 MVC5 中当使用   routes.MapMvcAttributeRoutes() 添加路由属性是导致在控制器创建的泛型方法调用错误:

Cannot call action method 'System.Collections.Generic.IEnumerable1[System.Web.Mvc.SelectListItem] GetSelectList[T](System.Collections.Generic.IEnumerable1[T], System.String, System.String, System.String, System.Object)' on controller 'PublicationSystem.Controllers.BaseController' because the action method is a generic method.
Parameter name: methodInfo

例如:

public void AddArbet<T>(T bet, int userId)
        {
            string key = "APIFilterArbet" + userId + "";
            List<T> bets = GetArbets<T>(userId);
            if (bets != null)
            {
                if (bets.Count > 50)
                {
                    bets.Clear();
                }
                bets.Add(bet);
                _cacheManager.Set(key, bets, 60);
            }
        }

  

解决方法: 只需要把  public void AddArbet<T>(T bet, int userId) 把 public ->  protected 即可 ,可以了下MVC 控制器继承工作原理。