Capitalizing the First Letter in All Keys of a JSON Object using C#

It took me some time to get this right, drawing from discussions on StackOverflow, but here is code that will capitalize the first letter of every key in a JSON object.

public static JObject Capitalize(JObject obj)
  JObject newObj = new JObject();
  foreach (var o in obj)
      string key = o.Key;
      string newKey = char.ToUpper(key[0]) + key.Substring(1);
      JToken value = o.Value;
      if (value.HasValues)
          JObject child = JObject.Parse(value.ToString());
          JObject newValue = Capitalize(child);
          value = newValue;
        catch (Exception)
      newObj[newKey] = value;
  return newObj;

Implementing a 410 Status Code Using C#

Here’s how you can setup a 410 Status Code using C#.  Just place this code in the code-behind file in the Page_Load event handler, replacing with your domain name, of course:

Response.StatusCode = 410;
if (Request.ServerVariables["SERVER_NAME"].ToString().ToLower() == "");
Response.Write("410 Gone");

Simple Code in C# to Convert DateTime object to the “datetime-local” type used in input controls with MongoDB Driver

As the title suggests, the following code converts a DateTime object to the “datetime-local” type used in HTML5 input controls (i.e. format 2016-10-30T00:00:00.00).  I developed this while working on a personal application using C# and MongoDB.  It uses the ToJson() function/method of the MongoDB Driver.

using MongoDB.Driver;
using System;

Datetime dt = new DateTime();
var dtLocal = dt.ToJson().ToString().Replace("ISODate(\"", "").Replace("Z\")", "");

Hopefully by posting this, it will help others save time.