C# Tips N Tricks: Saving Character Literals as String Characters

We have a requirement that when saving a specific type of string to the database it needs to save newline characters as a string (“\n”) not the character literal (‘\n’). It should have been straight forward but I found myself spinning in circles and decided to make a note on what the final code looked like.


	@Html.TextArea("Message", Model.Message, new { maxlength = 5000 })

Saving to DB:

      item.Message = model.Message.Replace("\r\n","\\n");

Pulling from DB:

      if (item != null)
        Message = item.Message.ToStringOrEmpty().Replace("\\n", "\r\n");
      if (Message.IsNullOrEmpty())
        Message = "Message goes here.\r\n\r\nSincerely,";

Visual Studio: Navigating large files

I am often working in Visual Studio in Controllers that have a large amount of code in them and want to look at code from multiple parts of the file. Using the split screen view is not sufficient.  The following is how I maximize my monitor space to get the best experience with the fewest distractions.

1. Select the tab you are working with.
2. Select Window -> New Window
3. Select Window -> New Vertical Tab Group
4. Press Shift – Alt – Enter to switch to full screen mode.

ReSharper, getting started again

Years ago I used to use ReSharper on a daily basis. Then I left that company and didn’t have easy access to it any more. I missed it. I finally am at a place where I have a copy again. I am in the process of relearning how to use it. Here are a few linking that I found useful:

1. This is ReSharpers PDF file the most used shortcuts
2. Pluralsights “ReSharper Fundamentals” course
3. 24 + 3 ReSharper tips by Rapid Application Development blog
4. A Code Project article with more detailed examples
5. ReSharper documentation of the various functionality

Lazy Programming: Lowercase URLs

Just so everyone knows, when it comes to programming I prefer to be as lazy as possible. This means that when I got my most recent story to render all routes in lowercase I decided fixing the Routing manually was way too much work. I spent some time on Google and discovered that the built-in lowercase functionality that is built into MVC 4 is broken and its a pain to work around. Instead I am adding a nuget package called LowercaseRoutesMVC to the project. It automatically renders routes in lowercase.  Unfortunately my coworker spent a couple hours yesterday working on updating his code to work manually.

Update: Quote from my boss to the coworker who did it manually yesterday, “hard work sometimes pays off tomorrow but procrastination always pays off immediately.”

MVC Tips n Tricks: Using a button like an action link

I had the need to display a button that works like an action link to redirect the user to a page within an Area and didn’t want to wrap the whole thing in a form just to do the redirect.  Here is what I used.  It allows the same styling and behavior as a button but redirects directly like an action link.

<input type="Submit" 
       value="Add Persona" 
       class="btn btn-primary btn-lg" 
       onclick="location.href='@Url.Action("Detail", "Persona", new { area = "ProfileInfo" })'" />

RegEx: Validating Phone Numbers

I am horrible at writing regular expressions but, like any developer, when I need them I want a good one.  Today I needed one to validate North American phone numbers, that would allow for all different kinds of formatting by the users and also allow for extensions.

On the following blog post, by Chris Nanney, I finally found the perfect one: 


I also found this site that I used to validate that it worked as I expected: http://regex101.com/

I’ve been in the position of having to take an unnormalized database that had virtually no data validation or standardization in place, and migrating it to a normalized schema. I used regex to help me through the process.

This post will deal specifically with phone numbers. The data I was importing had many problems: First, there was no standard formatting—some numbers were stored (xxx) xxx-xxxx, some xxx-xxx-xxxx, some xxx.xxx.xxxx, etc. Second, there wasn’t a separate field for extensions—they were just tacked on the end by either ext., EXT, x, Ex, or some variation. If there were only 20 numbers or so you could just fix them by hand, but you need an automated process to deal with say, 15,000.
via Cleaning Phone Numbers with Regular Expressions : Code : Chris Nanney.