Nitpicky ReSharper Behavior, Null Link with Request.Cookies

This is a really stupid little thing, but I am anally retentive, and when R # (6.1) emphasizes what I usually want to “fix”. I am working on an MVC4 project and passing the identifier value from one controller to another using a cookie. I'm still a little newbie to MVC, so there may be a better way to do this. In any case, I check for the presence of a cookie and then get its value as follows:

var idCookieString = string.Empty;
if(Request.Cookies.AllKeys.Contains("id"))
{
    idCookieString = Request.Cookies["id"].Value;
}

The annoyance is that R # raises Request.Cookies["id"]as a possible exception the link, although I check if the cookie exists in the instructions if. Is there a better way to check if a cookie exists before trying to recover it, or should I just put R # on it to ignore?

+3
source share
1 answer

R # does not know that Request.Cookies.AllKeys.Contains ("id") is actually a check to see if Request.Cookies ["id"] is there. Value will return anything. so yes, you need to either add to ignore R #, or add an extra line of useless code:

if (Request.Cookies["id"] != null)

afaik there is no other way.

+2
source

All Articles