find a location for property in a new city

Thursday, 25 August 2011

Azure Table Storage One of the request inputs is out of range

While trying to insert an entity into a Windows Azure Table Storage table I got the rather ambiguous "An error occurred while processing this request." error message from a System.Data.Services.Client.DataServiceRequestException. The inner exception help a message of "One of the request inputs is out of range."

The error message wasn't terribly helpful but I finally found what was causing the problem. It was because of my Row Key. The RowKey property that comes from the TableServiceEntity abstract class has specific requirements which I was unintentionally breaching.

My RowKey was calculated from another property of my Entity. In this particular instance this meant the string included a '/' symbol. This is not allowed by Table Storage for RowKeys.


When calculating the RowKey from the other property I am now removing special characters that the Azure Storage does not allow. This includes forward slash (/) character, backslash (\) character, number sign (#) character, question mark (?) character.

Old code:
private string productName;
public string ProductName
   get { return productName; }
   //setting the RowKey and productName to the same value in one go
   set { RowKey = productName = value; }
New code:
private string productName;
public string ProductName
   get { return productName; }
   set { RowKey = Regex.Replace(productName = value, @"[\ /?#]", ""); }
I actually already knew about this from when I read Apress's Windows Azure Platform but the confusing error message threw me off on a tangent and hoped it hadn't done the same to anyone else.

Follow britishdev on Twitter

Saturday, 13 August 2011

ASP.NET MVC Checkbox has a hidden input too

I noticed strange behaviour whilst coding and ASP.NET MVC CheckBox Html Helper. I noticed that the field which was in part of a GET form was creating the query string with the same field in it twice. When the box was checked one parameter was true and the other was false.

Say I have a boolean field, MyField, that I am creating a checkbox for. I would write this: @Html.CheckBox("MyField"). You would expect this to output a single check box but if you actually look at the HTML that is generated you will notice that is also creates a hidden field.

I found out the the reason for this is written within the ASP.NET MVC source code:
if (inputType == InputType.CheckBox) {
    // Render an additional  for checkboxes. This
    // addresses scenarios where unchecked checkboxes are not sent in the request.
    // Sending a hidden input makes it possible to know that the checkbox was present
    // on the page when the request was submitted.
    StringBuilder inputItemBuilder = new StringBuilder();

    TagBuilder hiddenInput = new TagBuilder("input");
    hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
    hiddenInput.MergeAttribute("name", name);
    hiddenInput.MergeAttribute("value", "false");
    return inputItemBuilder.ToString();

I appreciate the reasoning behind this but in my scenario MyField is part of a ViewModel that is being sent with the form and of course boolean values are False by default so this is wasted on my scenario.

Another reason I do not like this is because I am using the GET method on my form the user will see this oddity in the querystring. Worse still, they be a developer and judge my code as rubbish not knowing it is the doing of ASP.NET MVC. I can't have that! ;)


Simple solution really. Write the HTML you want in HTML, forget about the HtmlHelper:
<input type="checkbox" name="MyField" value="true" id="MyField"
@Html.Raw((Model.MyField) ? "checked=\"checked\"" : "") />

Remember the value="true" because the default value to be sent is "on" if it is checked, which obviously can't be parsed to a Boolean.

Obviously, this doesn't look that clean but I will continue to do this on forms that use the GET method.

Follow britishdev on Twitter