Modelbinding在ASP.NET MVC 2空的查询字符串参数

编程入门 行业动态 更新时间:2024-10-20 18:52:02
本文介绍了Modelbinding在ASP.NET MVC 2空的查询字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

行为described这里看来现在是ASP.NET MVC 2默认的(至少preVIEW 1)。

在modelbinding查询字符串是这样的:

富=&放大器;酒吧=猫

下面绑定发生(假设你与'富'和'酒吧'字符串属性绑定到一个模型)

ASP.NET MVC 1

model.Foo =; model.Bar =猫:

ASP.NET MVC 2(preVIEW 1至RC)

model.Foo = NULL; model.Bar =猫:

想给任何人谁是玩V2抬起头,因为这不是在的 GU-说明。也很好奇,如果任何人在知道可以对是否这将是最终实施或配置功能评论?我很好,无论哪种方式,但只希望他们不要切换回老样子!作为配置的就更好了。

编辑:从这个角度来吸取的教训是什么版本,你正在开发针对不写code,说Foo.Length == 0至测试空字符串或富。长度> 3,检查的最小长度。使用string.IsNullOrEmpty(美孚)和/或检查空第一。

更新:这个问题引起了我的好奇,为什么他们会实际上​​使这种变化。我想,我无意中发现了答案,同时研究禁用的控件。在W3 HTML规范定义了成功控制 如下:

  

一个成功的控制是有效的  提交。每一个成功的控制  已在其控制的名字搭配其  电流值作为所提交的一部分  形式的数据集。一个成功的控制  必须在FORM元素中定义  而且必须有控制的名字。

在换句话说 - 一个成功的控制是一个将使其返回到服务器作为查询字符串参数。现在,如果控制不具有有效的值,则根据该规范:

  

如果控制不具有电流值  当提交表单时,用户  剂不要求把它当作  一个成功的控制。

(与'不需要......在这里当场开间pretation的语言)

所以我觉得通过发送一个空的,而不是它减少了浏览器兼容性能空字符串,其中某些浏览器可能会发送富=放大器;酒吧= 和其他人可能根本不发送查询字符串参数。通过始终间preting 美孚= ,如果美孚是根本不存在的力量,你要更多的防守。

我觉得我至少在正确的轨道至于为什么在这里 - 和至少部分有事情做一个'succcessful控制的概念。

www.w3/ TR / HTML401 /交互/ forms.html#H-17.13.2

解决方案

空是它实际上是什么,以及它与其他可空类型,除了字符串兼容,所以我想这是由设计。

The behavior described here appears to now be the default for ASP.NET MVC 2 (at least for Preview 1).

When modelbinding a querystring like this :

?Foo=&Bar=cat

The following binding occurs (assuming you're binding to a model with 'Foo' and 'Bar' string properties)

ASP.NET MVC 1

model.Foo = ""; model.Bar = "cat":

ASP.NET MVC 2 (preview 1 through RC)

model.Foo = null; model.Bar = "cat":

Wanted to give anyone who is playing with V2 a heads up since this wasn't mentioned in the 'gu-notes'. Also curious if anyone in the know can comment on whether or not this will be the final implementation or a configurable feature? I'm fine either way but just hope they dont switch back to the old way ! Being configurable would be even better.

Edit: The lesson to learn from this point is whatever version you're developing against not to write code that says Foo.Length == 0 to test for an empty string or Foo.Length > 3 to check for a minimum length. Use string.IsNullOrEmpty(Foo) and/or check for null first.

Update: This question sparked my curiosity as to why they would actually make this change. I think that I stumbled on the answer while researching disabled controls. The W3 HTML spec defines a 'successful control' as follows :

A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

In other words - a successful control is one that will make it back to the server as a query string parameter. Now, if a control doesn't have a valid value then according to the spec :

If a control doesn't have a current value when the form is submitted, user agents are not required to treat it as a successful control.

(spot the 'open to interpretation' language here with 'not required to...')

So I think by sending a null instead of an empty string it reduces browser incompatibilites where certain browsers may send Foo=&Bar= and others may not even send that query string parameter. By always interpreting Foo= as if Foo wasn't there at all forces you to be more defensive.

I think I'm at least on the right track as to the reason why here - and at least in part has something to do with the notion of a 'succcessful control'.

www.w3/TR/html401/interact/forms.html#h-17.13.2

解决方案

Null is more representative of what it actually is, and it is compatible with other nullable types besides string, so I imagine it's by design.

更多推荐

Modelbinding在ASP.NET MVC 2空的查询字符串参数

本文发布于:2023-11-14 19:38:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   参数   ASP   Modelbinding   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!