IamCalvinBrown.com a Blog Site

Code Examples, Consulting Advice and lots of other cool things

How to remove special characters from a string in C#

public static string RemoveSpecialCharacters(string str)


            StringBuilder sb = new StringBuilder();

            foreach (char c in str)


                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')





            return sb.ToString();


Make all urls lowercase in MVC

The line highlighted in yellow sets all urls to lower case. this is more visually appealing to the users and less confusing with longer urls.

public static void RegisterRoutes(RouteCollection routes)



            routes.LowercaseUrls = true;




                   new { controller = "Home", action = "Index" }



                "Index", // Route name

                "{controller}/{action}/{id}", // URL with parameters

                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults



How to get Angular to display HTML

The important items to note here are two things. 1 is the angular-sanitize.min.js file which manages the html rendering, and the ng-bind-html attribute. It can be added to a div, span, etc, however I used it on a paragraph tag in this example.

The code that's displayed to the users

<div ng-app="myApp" ng-controller="myCtrl">

          <p ng-bind-html="description"></p>


The code (note: should be enclosed in a <script> tag

var app = angular.module('myApp', ['ngSanitize']);

var _desc = '<b>this is</b> sample html being displayed';

 app.controller('myCtrl', ['$scope' ,function ($scope) {

                $scope.description  = _desc;


Get a Querystring parameter in JQuery


var customerId = getParamByName('id');

 console.log("ID: " + customerId ); // write out the value

  function getParamByName(param) {

          paramparam.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");

          var regex = new RegExp("[\\?&]" + param+ "=([^&#]*)"),

          results = regex.exec(location.search);

          return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));


Adding a WebMethod to a .cs page

When you want code that can be called by JQuery inside your .cs page, you only need to add the [WebMethod] attribute.

Please note, the name of the method is erroneous, and this method returns no data. Have your function return a complex object (POCO), or a string. The Json below will deserialize it and return it to JQuery.


using System.Web.Services;



        public static string getSomeDataFromSomePlace()




                // method that returns data from the DB, a webservice, etc

string _objectOrPlainText = string.empty; // clearly this won't return anything -- go fetch data here


            catch (Exception ex)


                log.Error("SalonProfile:getSalonAmenitiesAndDescription", ex);


            string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(_objectOrPlainText);

            return json;


Selecting an item by name in a dropdown in JQuery

<select id="clients">

 <option value="1" >John</option>

  <option value="2">Calvin</option>

  <option value="3">Adjo</option>


// the list above is named clients, so the code below will select the item by name
$("#clients option:contains('Calvin')").attr('selected', true);

Setting the default page in web.config

Google wants the first page of websites to work with a simple / . To enable your site to be search friendly it's always best to set the default page that you site will render. Below is an example of how to do this:

      <defaultDocument enabled="true">    <!-- this line enables default documents for a directory -->
            <clear/>     <!-- removes the existing default document list -->                
            <add value="login.aspx"/>     <!-- login.aspx is now the default document  -->                
            <add value="index.aspx"/>     <!-- 2nd default document in search order -->                
            <add value="iamcalvinbrown.aspx/>     <!-- 3rd default document in search order -->
      <modules runAllManagedModulesForAllRequests="true"/>