-
Notifications
You must be signed in to change notification settings - Fork 190
Make cookies return null when the key doesn't exist. #462
Conversation
We have used to `if (Cookies["xxx"] == null)` to detect the key exist or not. The String.Empty will equals to "", We have to use Cookies.Keys.Contains("xxx") it's so complex.
Hi @kagamine, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! The agreement was validated by .NET Foundation and real humans are currently evaluating your PR. TTYL, DNFBOT; |
Should I add an Assert into
|
if (Cookies["xxx"] == null)
+1 on this change, missing should be null. I think Headers should behave this way as well. It's going to be a porting painpoint. For example, with Headers I have to use I assume there was discussion and reasoning behind this change in the first place though, looking forward to hearing the background. |
@davidfowl I don't see the not-there case really well thought out there. Compare old vs. new code. if (Request.Headers["MyHeader"] != null) New: if (!StringValues.IsNullOrEmpty(Request.Headers["MyHeader"])) There may be an extension method I can't find as well? If it's there, it would require a I think it's pretty hard to argue that it's an intuitive change - I'm just not seeing the wins here for the missing case. IMO, |
We could add an extension method actually that's not a bad idea but it's a struct so null won't cut it. |
True - I'd suggest the extension method be where intellisense will find it though, which would mean in already-used namespaces. IMO there's an argument to be made for pulling such basic types lower. In this case might I suggest a |
Use If you don't care about its value |
@NickCraver surely Old: if (Request.Headers["MyHeader"] != null) New: if (Request.Headers.ContainsKey("MyHeader")) Or: if (Request.Headers.TryGet("MyHeader", out header)) |
@kagamine rather than:
Try
Or if you want the value?
|
But in my computer, there was no ContainsKey with cookies. |
@benaadams We often check for existence then default in such scenarios, I suppose the new format would be: StringValues s;
var guid = Request.Headers.TryGetValue("Proxy-Guid", out s) ? s[0] : UnauthorizedGuid; Where as before we're talking about: var guid = Request.Headers["Proxy-Guid"] ?? UnauthorizedGuid; I see the wins on the allocation side, but it hurts on the usability is all I'm noting - you convinced me it's an overall good change here. IMO, it's an overall win since we're after the maximum performance ultimately as well. Semi-related: It would be really nice if the headers were ordered, since while the RFC (2616 - 4.2) says the overall key ordering doesn't matter (though individual values do) - it does matter practically. For example, we can detect a botnet faking a browser based on header order - since we know Chrome sends headers in a certain order. However, I am a practical person - I see how this wouldn't matter for 99.9999% of websites :) I'm just noting such things here that probably aren't considered in the discussions but are hit in production...or I'm totally wrong and things like this are brought up in the meetings. For cookies though (this PR), I think a |
In convention we used to Request.Headers["Proxy-Guid"] == null, not Request.Headers.ContainsKey. I think make the code simple is better. The new method is so complex. I still confused about today's change, why make it return String.Empty? |
+1 missing should be null |
/cc @Tratcher |
The cookies and headers issues are quite different due to the different data types being used, please open a separate bug to discuss handling missing headers. |
@kagamine Yes this needs tests. |
We used to
if (Cookies["xxx"] == null)
to detect the key exist or not.Now, if key
xxx
doesn't exist, it will return "".The String.Empty will equals to "", We have to use Cookies.Keys.Contains("xxx") to detect the key exist or not. it's so complex.
/cc @Eilon @troydai @JunTaoLuo