I'm trying to compare the content type of an HttpPostedFileBase
against an array of acceptable types, and I'm encountering some genuinely odd behaviour.
array.Contains()
can't find the string, but if I iterate over the collection and compare each entry to the string, it gets a match.
Here's my code:
string[] acceptable = new string[]{
"text/comma-separated-values",
"text/csv",
"text/anytext",
"application/csv",
"application/excel",
"application/octet-stream",
"application/vnd.ms-excel",
"application/vnd.msexcel"
};
string type = model.file.ContentType.ToLower().Trim();
string err = "acceptable.Contains(type) = " +
(acceptable.Contains(type) ? "true" : "false");
err += "<br/><br/>";
foreach (string s in acceptable)
{
if (s == type)
err += "but " + type + " is definitely in the string array!";
}
The code above outputs the following
acceptable.Contains(type) = false
but application/octet-stream is definitely present in the string array!
If I copy and paste the printed value of file.ContentType
into a string variable and run the comparison, it works fine. It only fails when reading the value directly from HttpPostedFileBase.ContentType
, which is of type System.String
by the way.