4

Resharperは「PossibleSystem.NullReferenceException」警告を表示しています。しかし、どうすれば入手できるのかわかりません。

public class PlaceController : PlanningControllerBase
{
    [Authorize]
    public ActionResult StartStop(int id)
    {
        if (Request != null && Request.Cookies != null && Request.Cookies["place"] != null)
        {
            if (Request.Cookies["place"].Value != null)//Possible NullReferenceException?
            {
                string placeInformation = Request.Cookies["place"].Value;//Possible NullReferenceException?
                //...
            }
        }
    }
}

すべてのフィールドをチェックした場合、これはどのようにNullReferenceを与えることができますか?以下を使用しても警告は表示されません。

Request.Cookies[0];//Index instead of name

編集:更新されたコード。

4

3 に答える 3

6

チェッカーは、CookieCollection インデクサーに渡される文字列の値が毎回同じであることをチェックしていないと思います。コードを次のように再構築すると想像できます。

if (Request != null && Request.Cookies != null) 
{
    var place = Request.Cookies["place"];
    if (place != null && place.Value == null) 
    { 
        string placeInformation = place.Value;
    } 
}

それはうまくいくかもしれません。

于 2010-03-04T15:33:20.907 に答える
4

すべての警告に耳を傾ける必要はありません。RequestオブジェクトとCookiesオブジェクトが null になることはないため、必要なのはこれだけです。

var placeCookie = Request.Cookies["place"]; 
if (placeCookie != null)
{
    string placeInformation = placeCookie.Value;
}
于 2010-03-04T15:47:51.977 に答える
0

今のところ、 placeInformationRequest.Cookies["place"].Value != null を null に設定するだけです。

于 2010-03-04T15:36:49.113 に答える