我收到空异常。当我直接兴奋这个页面时。我想处理空异常
C#
string json = "";
if (Request.QueryString["data"] !="")
{
json = Request.QueryString["data"];
var req = JsonConvert.DeserializeObject<Request>(json);//getting error in this line
string requestid = req.requestId;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYSTRING"].ConnectionString);
SqlCommand cmd = new SqlCommand();
connection.Open();
}
错误
值不能为空。 参数名称:值
最佳答案
大概 Request.QueryString["data"]
为空。您当前正在检查它是否是对空字符串的引用,而不是它是否是空引用。我怀疑你想使用 string.IsNullOrEmpty
检查:
string json = Request.QueryString["data"];
if (!string.IsNullOrEmpty(json))
{
var req = JsonConvert.DeserializeObject<Request>(json);
...
}
关于c# - C#中如何处理空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754769/