.net – 无法在控制器’Controller’上调用 *** 作方法’System.Web.Mvc.PartialViewResult Foo [T](T)’因为action方法是泛型方法

.net – 无法在控制器’Controller’上调用 *** 作方法’System.Web.Mvc.PartialViewResult Foo [T](T)’因为action方法是泛型方法,第1张

概述无法在控制器’Controller’上调用 *** 作方法’System.Web.Mvc.PartialViewResult FooT’,因为action方法是泛型方法 <% Html.RenderAction("Foo", model = Model}); %> ASP MVC 2有这种限制的解决方法吗?我真的更喜欢使用泛型.我提出的解决方法是将模型类型更改为对象.它有效,但不是首选: public P 无法在控制器’Controller’上调用 *** 作方法’System.Web.Mvc.PartialVIEwResult FooT’,因为action方法是泛型方法
<% HTML.RenderAction("Foo",model = Model}); %>

ASP MVC 2有这种限制的解决方法吗?我真的更喜欢使用泛型.我提出的解决方法是将模型类型更改为对象.它有效,但不是首选:

public PartialVIEwResult Foo<T>(T model) where T : class    {      // do stuff    }
解决方法 抛出的代码在默认的ActionDescriptor中:
internal static string VerifyActionMethodisCallable(MethodInfo methodInfo) {        // we can't call instance methods where the 'this' parameter is a type other than ControllerBase        if (!methodInfo.Isstatic && !typeof(ControllerBase).IsAssignableFrom(methodInfo.ReflectedType)) {            return String.Format(CultureInfo.CurrentUICulture,MvcResources.ReflectedActionDescriptor_CannotCallinstanceMethodonNonControllerType,methodInfo,methodInfo.ReflectedType.Fullname);        }        // we can't call methods with open generic type parameters        if (methodInfo.ContainsGenericParameters) {            return String.Format(CultureInfo.CurrentUICulture,MvcResources.ReflectedActionDescriptor_CannotCallOpenGenericmethods,methodInfo.ReflectedType.Fullname);        }        // we can't call methods with ref/out parameters        ParameterInfo[] parameterInfos = methodInfo.GetParameters();        foreach (ParameterInfo parameterInfo in parameterInfos) {            if (parameterInfo.IsOut || parameterInfo.ParameterType.IsByRef) {                return String.Format(CultureInfo.CurrentUICulture,MvcResources.ReflectedActionDescriptor_CannotCallMethodsWithOutOrRefParameters,methodInfo.ReflectedType.Fullname,parameterInfo);            }        }        // we can call this method        return null;    }

因为代码调用“methodInfo.ContainsGenericParameters”,所以我认为没有办法在不创建自己的ActionDescriptor的情况下覆盖此行为.从浏览源代码看,这似乎并非无足轻重.

另一个选择是使您的控制器类通用并创建一个自定义通用控制器工厂.我有一些实验代码可以创建一个通用控制器.它的Hacky但它​​只是一个个人实验.

public class GenericControllerFactory : DefaultControllerFactory{    protected overrIDe Type GetControllerType(System.Web.Routing.RequestContext requestContext,string controllername)    {        //the generic type parameter doesn't matter here        if (controllername.EndsWith("Co"))//assuming we don't have any other generic controllers here            return typeof(GenericController<>);        return base.GetControllerType(requestContext,controllername);        throw new InvalIDOperationException("Generic Factory wasn't able to resolve the controller type");    }    protected overrIDe IController GetControllerInstance(System.Web.Routing.RequestContext requestContext,Type controllerType)    {        //are we asking for the generic controller?        if (requestContext.RouteData.Values.ContainsKey("modelType"))        {            string typename = requestContext.RouteData.Values["modelType"].ToString();            //magic time            return GetGenericControllerInstance(typename,requestContext);        }        if (!typeof(IController).IsAssignableFrom(controllerType))            throw new ArgumentException(string.Format("Type requested is not a controller: {0}",controllerType.name),"controllerType");        return base.GetControllerInstance(requestContext,controllerType);    }     /// <summary>    /// Returns the a generic IController tIEd to the typename requested.      /// Since we only have a single generic controller the type is hardcoded for Now    /// </summary>    /// <param name="typename"></param>    /// <returns></returns>    private IController GetGenericControllerInstance(string typename,RequestContext requestContext)    {        var actionname = requestContext.RouteData.Values["action"];        //try and resolve a custom vIEw model        Type actionModelType = Type.GetType("Brainnom.Web.Models." + typename + actionname + "viewmodel,Brainnom.Web",false,true) ??             Type.GetType("Brainnom.Web.Models." + typename + ",true);        Type controllerType = typeof(GenericController<>).MakeGenericType(actionModelType);        var controllerBase = Activator.CreateInstance(controllerType,new object[0] {}) as IController;        return controllerBase;    }}
总结

以上是内存溢出为你收集整理的.net – 无法在控制器’Controller’上调用 *** 作方法’System.Web.Mvc.PartialViewResult Foo [T](T)’因为action方法是泛型方法全部内容,希望文章能够帮你解决.net – 无法在控制器’Controller’上调用 *** 作方法’System.Web.Mvc.PartialViewResult Foo [T](T)’因为action方法是泛型方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1117090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存