为什么resharper认为asp.net Request["abc"]永远不能为null?

问题描述 投票:0回答:1

我正在使用 ASP.NET Web 表单,并且我有一个像这样的片段:

string errorId = Request["errorid"];
if (errorId != null) ...

Resharper 表示第二行“表达式始终为真”,并希望删除该条件。

文档https://msdn.microsoft.com/en-us/library/system.web.httprequest.item(v=vs.110).aspx明确表示如果找不到指定的key,则为NULL已返回。

为什么 R# 认为索引器永远不能返回 null?除了忽略警告之外,我还能做些什么来解决这个问题吗?

撰写本文时我使用的是最新版本(2017.3.1)

c# resharper
1个回答
1
投票

我发布的代码片段不完整;实际的代码更像是这样的:

string errorId = Request["errorid"];
log.Debug(HttpUtility.HtmlEncode(errorId));
if (errorId != null) ...

R# 正确地计算出,如果 errorId 为 null,则“log”行将引发异常,因此当它到达“if”行时,errorId 不能为 null。

© www.soinside.com 2019 - 2024. All rights reserved.