public class HomeController : Controller
{
public ActionResult Index()
{
// Warning 19 CodeContracts: Possibly calling a method on a null reference 'Website.Controllers.HomeController.<Index>o__SiteContainer0.<>p__Site2.Target' HomeController.cs
if (ViewBag != null)
{
ViewBag.Message = "Be Immortal";
}
return View();
}
public ActionResult About()
{
return View();
}
}
绞尽脑汁想弄清楚如何在 MVC 3 中满足 ViewBag 的 [X] 隐式非 Null 义务。有没有人想出一种方法来使代码合同与新的动态 ViewBag 类型相匹配?
我希望能够将 ViewBag 包装在基本控制器中,如 ViewBagSafe 等。
我确实意识到这对项目来说并不是一个真正的问题,因为 ViewBag 永远不会为空,但我想保留代码合同,默认为空检查,以防我将来的失误(并且仍然能够在没有警告的情况下编译这样我就可以轻松识别我自己的违约编码)。