Admittedly, I am no Brad Abrams when it comes to API design and usage but I am pretty sure that this API faux pas constitutes itself as a bug. The bug is in HttpCookie and has to do with the Value and Values properties. You can use the following code to reproduce the bug:
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write(Response.Cookies[ "cooookie" ].Value == null);
Response.Write(”");
Response.Write(Response.Cookies[ "cooookie" ].Values.ToString());
Response.Write(”");
Response.Write(Response.Cookies[ "cooookie" ].Value == null);
}
By looking at the code you would expect the output of the page to be:
True
True
However, it wouldn’t be a very good bug if it were to work as expected. Instead of the above you get:
True
False
So how can this be? Why would merely reading the value of the Values property change the value in Value property? The problem is actually somewhat interesting and has to do with way these two properties try and show you the same thing (which begs the question as to why you need them both?). If you look at the framework code for HttpCookie you’ll see that the bug is in the way the getters work.
First. look at the getter for the Value property. Here is (essentially) the code for that getter (_multiValue is a private HttpValueCollection used by the Values property) :
get
{
if (this._multiValue != null)
{
return this._multiValue.ToString(0);
}
return this._stringValue;
}
The idea here is to show a string representation of the HttpValueCollection if we have one and if not, then show the single string value. Next look at the getter for the Values property:
get
{
if (this._multiValue == null)
{
this._multiValue = new HttpValueCollection();
if (this._stringValue != null)
{
// some code to add it
}
}
return this._multiValue;
}
You can see that the key point here is that the property creates an instance of _multiValue if one does not exist. This is bad for the other property (Value) because that was our condition to determine what to output. But why does this matter? It only matters because we are using ToString(bool) on the HttpValueCollection which returns “” when the value is null! This is why our return value changes upon getting the Values property. I guess a better way to do this would be to use the same private variable for both properties since they are attempting to represent the same thing. So you would create an instance of the collection in the setter of Value and just add the value there.
.NET
I am attempting to build a totally anonymous feedback system here at work and so I needed to find a way to prevent clients from caching any data coming off the server. I started out by just putting in the standard cache killing code in the actual pages I was using but then I realized that this didn’t prevent the image, css, and javascript files from being cached off the server. So to solve this issue I just made a little HttpModule to prevent caching of any content coming off the server. Here is the code:
using System;
using System.Web;
namespace TT.Web.HttpModules
{
///
/// HttpModule to prevent caching
///
public class NoCacheModule : IHttpModule
{
public NoCacheModule()
{
}
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.EndRequest += (new EventHandler(this.Application_EndRequest));
}
public void Dispose()
{
}
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays( -100 );
context.Response.AddHeader( “pragma”, “no-cache” );
context.Response.AddHeader( “cache-control”, “private” );
context.Response.CacheControl = “no-cache”;
}
#endregion
}
}
The last thing to try and tackle would be preventing the pages from showing up in history as well… but I doubt there is anything I can do about that.
.NET
Ever since I was wee programmer I was taught that “good” gui design (as it related to controls) meant that you align labels and control right next to each other. So if one were to create a data entry screen it would like so:
Since moving to web development I have seen a lot of web pages from some big companies (like these examples; HP, MS, SUN, etc..) designed without aligning the labels next to the controls so they look like this:
I continue to think the first approach is better because it seems like your eyes have to do less horizontal scrolling to figure out what to put in the given textbox. However, I began to wonder if “things” have changed and that the old way of doing things isn’t the preferred way anymore (especially since these bigger companies were doing it “wrong“). Needless to say I was happy when I saw in the BLAF guidelines (mentioned in an earlier post) that they right align the labels next to the control they are for as well. I wonder why some web developers choose to align their controls this way? My guess; if you dont specify an alignment it goes left… so people left it there and now we think thats the way to do it.
I understand that Oracles BLAF isn’t the definitive standard (is there one?) but it’s always nice to find something that supports your way of doing things!
.NET
Outlook rules frustrate me to no end; especially when using exchange! I have tried and tried in the past to build a rule that would route all of the internal email sent to me into a special folder for internal communication with absolutely no success. I like to route every email message I get to a folder so that I know what it’s about without having to even look at it. Most of the rules work without a hitch… but when it came to building a filter for the “Work“ folder… no success. I think I tried every single filter criteria twice and none of them were able to successfully filter only internal messages. The closest one was using the “Sender is in specified address book“ filter. However, that filter runs client side only (who knows why) which is no good because my blackberry and owa would not get filtered messages unless my outlook was open.
Today, however I felt a renewed sense of confidence in my battle against outlook and decided to try yet again at winning this war. I called in reinforcements (Ryan – who has also been waging the same war) and we began to look at our options for getting the filter to work. Just when we thought we were once again defeated it came to us! Instead of using an inclusive filter to try and find the messages we needed to an exclusive one. So we let all messages through and then only route the ones that do not have the word “Received” in the header (since exchange messages have no headers). walla! it worked.
The finished rule:
Apply this rule after a message arrives
move it to the TT folder
except if the message header contains Received
Ryan also pointed out that outlook runs a lot faster now as well since we don’t have that huge client side rule checking the address book for each message.
Misc
I was doing a little leg work for project TruckerHat (a significant re-design project I am tasked with this quarter) and came across a nice site put together by Oracle affectionately called BLAF (browser look and feel). The site contains some pretty extensive guidelines about designing web pages all the way down to spacing between elements. They even have some templates and a huge list of icons.
The link to the site is: http://otn.oracle.com/tech/blaf/specs/index.html
.NET
It’s been a while since I ran into this “bug”, but the other day I trying to figure out why the images in my email were not showing up. So I did what any normal human would do on a Wednesday and tried to do a “View Source” on the HTML content. When I clicked on the view source option in the context menu however I got no source. I didn’t even get an error. Then I tried IE. No love there either. Then something happened to me that very rarely happens; I remembered something from more than a week ago. This very thing has happened to me before, about 2 years ago.
The answer? In order to be able to view source again you have to clear out your temporary internet files! The answer to this riddle has always baffled me. Why does the fact that your temporary internet files allocation has reach capacity effect your ability to see the source of the page that is clearly stored on your machine someplace? One would think that once you reach the allocated capacity, that IE would begin deleting the oldest content to make more room. Not the case when making room for view source I guess.
So after deleting the temp files all was good again.
Misc
Well I found out today that the C# compiler only allows 2046 characters on one line. I wonder why this is the case?
Why was I putting 2046+ characters on one line you ask? Well I was pasting in a rather large error message so I could play an April fools joke on Richard who was kind enough to roll out code on April 1st. Nothing like having the CEO generate a hundred or so errors on the code you just rolled out!
Of course Richard being the astute debugging machine that he is, caught on to the joke pretty quickly.
Stack Trace:
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaiseAprilFoolsEvent(IFoolRichard target, String gotcha)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain()
|
.NET, Misc
April Fools, compiler