如果一个尝试,以防止空引用例外/索引越界异常的MVC的意见? [英] Should one try to guard against null reference exceptions / index out of bounds exceptions in MVC views?

查看:95
本文介绍了如果一个尝试,以防止空引用例外/索引越界异常的MVC的意见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发表评论,以这个问题的接受的答案,现在,背后的有没有意见抛弃code?那么,你要测试?的,指出它只要你添加一个在我看来,

I want to post a comment to this question's accepted answer, "Haven't views abandoned code behind now? So what are you going to test?" pointing out that it seems to me as soon as you add an

<% if (Model.Thing == "abc") {} %>

@if (Model.Thing == "abc") {}

你的看法,存在着潜在的东西炸毁,而且潜力应被防御。

to your view, there exists the potential for something to blow up, and that potential should be guarded against.

在关于我挂的问题,我可以看到正在作出一种观点认为应该警惕空引用例外,在code-背后的可能性,而不是乱抛垃圾的人的使用和null检查的观点,但再怎么关于局部视图的情况下?它真的会更好地添加多个null检查在许多地方的局部视图可能被渲染,而不是一个地方,在视图本身?

In regard to the question I linked to, I could see an argument being made that one should guard against the possibility of null reference exceptions in the code-behind rather than littering one's view with null checks, but then how about the case of partial views? Would it really be better to add multiple null checks in the numerous places where a partial view might be rendered, rather than one place, in the view itself?

推荐答案

海事组织,你应该只防范空,指数OOB等,在你的看法,如果你期待值是零,OOB等。

IMO, you should only guard against null, index oob, etc, in your views if you are expecting values to be null, oob, etc.

在理想情况下,你应该有单元测试您的操作方法,从而确保某型号值不为空/在边界/等。当有一个可能性,即值为null,那么你可能有一个很好的理由,一个空检查在视图中。否则,它是无用的code。

Ideally you should have unit tests for your action methods that ensure certain model values are not null / in bounds / etc. When there is a possibility that a value will be null, then you might have a good reason for a null check in a view. Otherwise, it's useless code.

这篇关于如果一个尝试,以防止空引用例外/索引越界异常的MVC的意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆