MVC 操作参数不一致地绑定到可为 null 的 Guid

发布于 2024-12-14 20:21:15 字数 308 浏览 2 评论 0原文

这看起来确实是一个框架错误。该参数位于请求的参数中,具有正确的名称,但它并不总是绑定到操作参数。它运行了 6 个月,但现在整个应用程序的多个操作方法都发生了这种情况。

我可以关闭 VS 或重新启动计算机,通常可以修复它。最近,当我遇到这个问题时,如果不将参数转换为字符串然后转换为 GUID,我就无法解决这个问题。

关于如何处理此问题的任何建议,因为我们有许多接受可为空的 Guid 的操作方法,并且必须手动转换参数很痛苦。

我不想为此找到解决方法,我想知道是否有人知道我如何调试这个或他们认为可能发生的情况。它是随机且不一致的。

谢谢!

This really seems like a framework bug. The parameter is in the request's params with correct name but it doesnt always bind to the action parameter. It worked for 6 months, but now this is happening in several action methods across the application.

I was able to either close VS or restart computer and that would usually fix it. Lately when i've run into this i couldnt get by it without turning the paramter into a string and then converting to a GUID.

Any advice on what to do with this since we have many action methods accepting nullable Guids and its a pain to have to manually convert the parameter.

I dont want a workaround for this, i want to know if anyone knows how i could debug this or what they think might be going on. It is random and inconsistent.

thanks!

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

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

发布评论

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

评论(2

你没皮卡萌 2024-12-21 20:21:15

我自己实际上没有遇到过这个问题,我最初的想法是这是从浏览器传入的实际值的问题。

我已经使用可为空的 DateTimes 调试了类似的问题,并且能够通过编写自定义模型绑定程序并将其设置为仅绑定 DateTime 来解决它?类型。

通过这样做,您可以检查传入的值并检查它们是否有任何异常。

这非常简单。在你的global.asax:

ModelBinders.Binders.Add(typeof(guid?), new GuidModelBinder());

然后创建一个类

public class GuidModelBinder : IModelBinder
    {

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            var modelState = new ModelState { Value = valueResult };
        }
        //Do whatever you need to inspect the valueResult in here
    }

Without actually having had this problem myself my initial thoughts would be that this is a problem with the actual values being passed in from the browser.

I've debugged a similar issue like this with nullable DateTimes and was able to get around it by writing a custom model binder and setting it up to only bind DateTime? types.

By doing this, you can inspect the values getting passed in and check them for any anomalies.

It's pretty straight forward to do. In you global.asax:

ModelBinders.Binders.Add(typeof(guid?), new GuidModelBinder());

And then create a class

public class GuidModelBinder : IModelBinder
    {

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            var modelState = new ModelState { Value = valueResult };
        }
        //Do whatever you need to inspect the valueResult in here
    }
如何视而不见 2024-12-21 20:21:15

这确实是非常奇怪的行为。要调试它,您可能需要查看 ASP.NET MVC 的源代码。然后您可以“步入”ModelBinder 的方法。

您可能还想尝试创建自定义模型绑定器。首先,如果您想单步执行 MVC 的内置方法,这将为您提供一个更容易放置断点的位置。但它也让您有机会记录正在发生的情况(例如,识别绑定失败的情况),以及在识别问题根源后可能使用您自己的代码来确保模型正确绑定。

在您走得太远之前,您可能应该检查您是否已经在使用自定义模型绑定器,如果是,请检查它是否存在潜在的错误。您还应该确保您的模型类尽可能简单,并且不受可能的竞争条件或其他奇怪情况的影响。

That is, indeed, very strange behavior. To debug it, you may want to check out ASP.NET MVC's source code. You could then "step into" the ModelBinder's methods.

You may also want to try creating a custom model binder. First of all, this would give you an easier place to put a break point if you want to step into MVC's built-in methods. But it would also give you a chance to log what's happening (to identify the cases where the binding fails, e.g.), as well as potentially using your own code to ensure the model binds correctly once you identify the root of the problem.

Before you go too far, you should probably check whether you're already using a custom model binder, and if you are, check it for potential bugs. You should also ensure that your model class is as simple as it can possibly be, and not subject to possible race conditions or other weirdness.

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