C# Convert date from UTC to EST

Published on Jun 7, 2017 by Jamie Munro

Every now and then I need to convert a date from Coordinated Universal Time (UTC) to Eastern Standard Time (EST).† I always found myself trying to remember "Where did I use this last?".† To avoid asking myself, I have now immortalized it on my blog.

Read more >

Tags: ASP.NET | c# | timezones

How to return a CSV from ASP.NET Web API using C#

Published on May 4, 2017 by Jamie Munro

ASP.NET Web API is a framework that is built upon the MVC (Model View Controller) principal to assist in rapidly building a RESTful API using the .NET framework.


By default Web API makes it easy to return common HTML content types, such as: JSON and XML.† However, it requires a little bit more work to return a CSV using Web API.† Read on to learn how.

Read more >

Tags: ASP.NET | c# | web api | csv

Unit Testing Data Validation with MVC

Published on May 11, 2015 by Jamie Munro

Problem


You've added some data validation and you want to test it out in action. The data validation can be standard data annotations, custom data attributes, or data validation implemented via an IValidatableObject.

Solution


Unit testing data validation can be accomplished by creating a ValidationContext in conjunction with the Validator helper class. To demonstrate how this is accomplished, I will use the ValidationModel class I created in the Removing Data Validation from MVC Controllers. As you may recall this class contained an IValidatableObject that ensured if the boolean FixedQuantity was true, the MaxQuantity property must be null.

Read more >

Tags: validation | ASP.NET | mvc | Testing | unit testing | ivalidatableobject | validationcontext

Removing Data Validation from MVC Controllers

Published on May 6, 2015 by Jamie Munro

Problem


You require custom data validation that cannot be accomplished via the built-in data annotation validation attributes - or - your data validation requires access to multiple properties in your model.

Solution


Many times the easy answer is to place the validation inside of your controller. However, I'm a strong believer of placing data validation outside of your controllers and within your data model. This is commonly accomplished by adding data annotation above the properties, such as the [Required] attribute. Another common way is to create your own validation attributes.

This example will use a third way and that is to implement the IValidatableObject interface. This interface defines a Validate function that must be implemented and because it is inside of your data model it has access to all properties within your model.

Read more >

Tags: validation | ASP.NET | mvc5 | ivalidatableobject | validationcontext

Integrating Automapper with an MVC Result Filter

Published on May 4, 2015 by Jamie Munro

Problem


On a variety of actions inside your controllers you call the Automapper NuGet Package to convert from one model to another and you do not want to continue repeating this code each time.

Solution


Applying an MVC result filter to the action can execute the Automapper code after the action has been processed by the MVC framework and before the view is rendered.

Please note this is an excerpt from Chapter 9 of my ASP.NET MVC 5 with Bootstrap and Knockout.js book. In this chapter I provide a brief overview of the 5 different MVC filters and then over the next two chapters provide detail examples of each. The following post is leveraging the example of a Result Filter.

Read more >

Tags: ASP.NET | mvc | automapper | mvc5

Speeding up my unit tests

Published on Apr 28, 2014 by Jamie Munro

Advanced Automatic Ninject Bindings

Published on Jan 13, 2014 by Jamie Munro

Automatic Ninject Bindings

Published on Jan 7, 2014 by Jamie Munro

Automapper Performance Testing

Published on May 27, 2013 by Jamie Munro

Why the Repository Pattern

Published on May 20, 2013 by Jamie Munro

The Best Way to Prevent SQL Injection

Published on Sep 19, 2012 by Jamie Munro

What I Learned This Summer

Published on Sep 7, 2012 by Jamie Munro

MVC 3 Routing Example

Published on Oct 17, 2011 by Jamie Munro

20 Recipes for Programming MVC 3

Published on Oct 12, 2011 by Jamie Munro