【问题标题】:Passing parameters to modelbinder on action parameter在动作参数上将参数传递给模型绑定器
【发布时间】:2009-01-08 08:35:19
【问题描述】:

我希望使用我直接在动作方法参数上制作的模型绑定器。如:

public ActionResult MyAction([ModelBinder(typeof(MyBinder))] string param1)

但是,我需要将一个字符串传递给活页夹本身,所以我想知道您是否可以按照以下方式做一些事情:

public ActionResult MyAction([MyBinder("mystring")] string param1)

有可能吗?

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    不,您不能通过属性声明传递参数。如果您查看 ModelBinderAttribute 的源代码,您会发现它的构造函数只接受一个类型参数,它没有其他属性,并且它是一个密封类。所以这条路是死路。

    据我所知,将信息输入 ModelBinder 的唯一方法是 FormCollection 本身。

    但是,您可以创建一个父绑定器类型并为您打算使用的每个参数值设置子类型。它很乱,但它可以在你给出的例子中工作。

    【讨论】:

    • 您可以在 POST 上获取 url 和查询字符串参数,而不仅仅是表单集合,因为您可以访问模型绑定器中的 httpcontext
    • @monkeylee modelbinder 没有为您提供 FormCollection,但您可以从 Request.Form 中创建一个新的。 FormCollection 只是一个包装器。在这里查看@haacked 的答案stackoverflow.com/questions/1510946/…
    【解决方案2】:

    是的,这是可能的。您应该创建一个从System.Web.Mvc.CustomModelBinder 驱动的类,并覆盖方法GetBinder。例如,这里的实现除了需要一个Type 对象外,还需要一个用于模型绑定器构造函数参数的对象数组:

    public sealed class MyModelBinderAttribute : CustomModelBinderAttribute
    {
        /// <summary>
        /// Gets or sets the type of the binder.
        /// </summary>
        /// <returns>The type of the binder.</returns>
        public Type BinderType { get; }
        /// <summary>
        /// Gets or sets the parameters for the model binder constructor.
        /// </summary>
        public object[] BinderParameters { get; }
    
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Web.Mvc.ModelBinderAttribute" /> class.
        /// </summary>
        /// <param name="binderType">The type of the binder.</param>
        /// <param name="binderParameters">The parameters for the model binder constructor.</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="binderType" /> parameter is null.</exception>
        public MyModelBinderAttribute(Type binderType, params object[] binderParameters)
        {
            if (null == binderType)
            {
                throw new ArgumentNullException(nameof(binderType));
            }
    
            if (!typeof(IModelBinder).IsAssignableFrom(binderType))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                    "An error occurred when trying to create the IModelBinder '{0}'. Make sure that the binder has a public parameterless constructor.",
                    binderType.FullName), nameof(binderType));
            }
            this.BinderType = binderType;
            this.BinderParameters = binderParameters ?? throw new ArgumentNullException(nameof(binderParameters));
        }
    
        /// <summary>Retrieves an instance of the model binder.</summary>
        /// <returns>A reference to an object that implements the <see cref="T:System.Web.Mvc.IModelBinder" /> interface.</returns>
        /// <exception cref="T:System.InvalidOperationException">An error occurred while an instance of the model binder was being created.</exception>
        public override IModelBinder GetBinder()
        {
            IModelBinder modelBinder;
            try
            {
                modelBinder = (IModelBinder)Activator.CreateInstance(this.BinderType, this.BinderParameters);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                    "An error occurred when trying to create the IModelBinder '{0}'. Make sure that the binder has a public parameterless constructor.",
                    this.BinderType.FullName), ex);
            }
            return modelBinder;
        }
    }
    

    【讨论】:

      【解决方案3】:

      只是一个想法,我正在考虑需要类似的要求,我想通过城堡服务。

      我想知道您是否可以使用传递给 Binder 的 ControllerContext 并通过属性公开服务/属性或其他方式?

      只是一个想法,我现在要试试这个,我会反馈的

      丰富

      【讨论】:

        猜你喜欢
        • 2012-10-05
        • 1970-01-01
        • 2014-09-12
        • 1970-01-01
        • 1970-01-01
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多