从 cookie 问题中提取的自定义模型绑定器?

发布于 2024-08-21 00:33:16 字数 819 浏览 10 评论 0原文

我正在尝试执行以下操作。

使用默认模型绑定器从查询字符串值绑定对象。
如果失败,我会尝试从 cookie 值绑定对象。

但是,我在该对象上使用数据注释,但遇到以下问题。

  1. 如果没有查询字符串参数,默认模型绑定器甚至不会在必填字段上注册任何验证错误。如果属性本身不在查询字符串集合中,它显然不会触发这些验证器。我怎样才能改变这种行为?如果必填字段不在查询字符串中,我希望它们是错误的。
  2. 如果确实有模型验证错误,我想从 cookie 加载模型,然后重新验证对象。我不确定如何让模型绑定器验证我自己填充的对象。

这是我到目前为止所拥有的。

    public class MyCarBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var myCar = base.BindModel(controllerContext, bindingContext);

        if (!bindingContext.ModelState.IsValid)
        {
            myCar = MyCar.LoadFromCookie();
            // Not sure what to do to revalidate
        }

        return myCar;
    }
}

任何有关如何正确执行此操作的帮助将不胜感激。

I am trying to do the following.

Use the default model binder to bind an object from query string values.
If that fails, I then try and bind the object from cookie values.

However I am using dataannotations on this object and I am having the following problems.

  1. If there are no querystring parameters the default model binder doesn't even register any validation errors on required fields. It apparently doesn't even fire these validators if the property itself is not in the query string collection. How can I change this behavior? I would like the required fields to be errors if they aren't in the query string.
  2. If I do have model validation errors, I would like to then load the model from the cookie and then revalidate the object. I am not sure how to get the model binder to validate an object I have populated myself.

Here is what I have so far.

    public class MyCarBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var myCar = base.BindModel(controllerContext, bindingContext);

        if (!bindingContext.ModelState.IsValid)
        {
            myCar = MyCar.LoadFromCookie();
            // Not sure what to do to revalidate
        }

        return myCar;
    }
}

Any help on how to properly do this would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-08-28 00:33:16

嗯,我自己解决了。在此发布解决方案,以防有人有意见或可能想使用它。

 public class MyCarBinder : DefaultModelBinder
 {
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var queryStringBindingContext = new ModelBindingContext()
        {
            FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix,
            ModelMetadata = bindingContext.ModelMetadata,
            ModelName = bindingContext.ModelName,
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = new QueryStringValueProvider(controllerContext),
            ModelState = new ModelStateDictionary()
        };

        var myCar = base.BindModel(controllerContext, queryStringBindingContext);

        if (queryStringBindingContext.ModelState.IsValid)
            return myCar;

        // try to bind from cookie if query string is invalid
        var cookieHelper = new Helpers.ControllerContextCookieHelper(controllerContext);
        NameValueCollection nvc = cookieHelper.GetCookies(Helpers.CookieName.MyCar);

        if (nvc == null)
        {
            bindingContext.ModelState.Merge(queryStringBindingContext.ModelState);
            return myCar;
        }

        var cookieBindingContext = new ModelBindingContext()
        {
            FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix,
            ModelMetadata = bindingContext.ModelMetadata,
            ModelName = bindingContext.ModelName,
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = new NameValueCollectionValueProvider(nvc, CultureInfo.InvariantCulture),
            ModelState = new ModelStateDictionary()
        };

        var myCarFromCookie = base.BindModel(controllerContext, cookieBindingContext);

        if (cookieBindingContext.ModelState.IsValid)
        {
            MyCar temp = myCarFromCookie as MyCar;
            if (temp != null)
                temp.FromCookie = true;

            return myCarFromCookie;
        }
        else
        {
            bindingContext.ModelState.Merge(queryStringBindingContext.ModelState);
            return myCar;
        }
    }
}

Well, I solved it myself. Posting the solution here in case anyone has a comments or might like to use it.

 public class MyCarBinder : DefaultModelBinder
 {
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var queryStringBindingContext = new ModelBindingContext()
        {
            FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix,
            ModelMetadata = bindingContext.ModelMetadata,
            ModelName = bindingContext.ModelName,
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = new QueryStringValueProvider(controllerContext),
            ModelState = new ModelStateDictionary()
        };

        var myCar = base.BindModel(controllerContext, queryStringBindingContext);

        if (queryStringBindingContext.ModelState.IsValid)
            return myCar;

        // try to bind from cookie if query string is invalid
        var cookieHelper = new Helpers.ControllerContextCookieHelper(controllerContext);
        NameValueCollection nvc = cookieHelper.GetCookies(Helpers.CookieName.MyCar);

        if (nvc == null)
        {
            bindingContext.ModelState.Merge(queryStringBindingContext.ModelState);
            return myCar;
        }

        var cookieBindingContext = new ModelBindingContext()
        {
            FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix,
            ModelMetadata = bindingContext.ModelMetadata,
            ModelName = bindingContext.ModelName,
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = new NameValueCollectionValueProvider(nvc, CultureInfo.InvariantCulture),
            ModelState = new ModelStateDictionary()
        };

        var myCarFromCookie = base.BindModel(controllerContext, cookieBindingContext);

        if (cookieBindingContext.ModelState.IsValid)
        {
            MyCar temp = myCarFromCookie as MyCar;
            if (temp != null)
                temp.FromCookie = true;

            return myCarFromCookie;
        }
        else
        {
            bindingContext.ModelState.Merge(queryStringBindingContext.ModelState);
            return myCar;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文