c# - 检查 Null 异常的正确方法是什么?

标签 c# asp.net

哪个是最正确的代码?

if (HttpContext.Current.Response.Cookies[authCookieName] != null) {
    HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
}

if (HttpContext.Current != null)
    if (HttpContext.Current.Response != null)
        if (HttpContext.Current.Response.Cookies != null)
            if (HttpContext.Current.Response.Cookies[authCookieName] != null)
                HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";

最佳答案

如果 HttpContext、HttpContext.Current、HttpContext.Current.Response 或 Http.Current.Response.Cookies 中的任何一个为 null,那么您就已经有麻烦了。让异常发生并修复您的网络服务器。

关于c# - 检查 Null 异常的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/165458/

相关文章:

c# - DBTransaction 回滚抛出

html - 在 aspx 页面中包含 html 文件

c# - 在 RESTful WebApi 请求上运行后台线程

javascript - 长字符串在 Firefox 中被自动 chop ,而在谷歌浏览器中它完美地工作

c# - 从 Google Android Management API 控制已注册 MDM 设备上的应用程序版本

c# - IronPython 有时导入模块但不导入其他模块

c# - 从 C# 中的 SelectList 中删除项目

c# - 为什么在尝试删除文件时会出现参数异常?

asp.net - 在 IIS 中将经典的 asp 页面映射到 .net

asp.net - 白色标签现有的 asp.net web 表单站点