Render HTML in a Validation Message in ASP.NET MVC

Often times you may want to render a custom validation message that contains HTML markup.  If you simply add the line:


This will encode the markup so that less than signs and greater than signs are rendered < and >, respectively.  This, of course, is not what you want.

To render your HTML markup, its a little dirty, but it does work.

Simply add the following to your view:

@Html.Raw(HttpUtility.HtmlDecode(Html.ValidationMessageFor(m => m.Email).ToHtmlString()))

Where "Email" is a property of your model.

FileNotFoundException: Could not load file or assembly 'Microsoft.Web.Infrastructure'

When deploying an MVC application to a hosting environment you may receive the error:

[FileNotFoundException: Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]

This is also one of a few component libraries that are needed for deploying an MVC application:

  • System.Web.Helpers.dll (required by the web.config)
  • System.Web.Mvc.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll
  • Microsoft.Web.Infrastructure.dll

The system libraries are installed with .NET 4, however, 'Microsoft.Web.Infrastructure.dll' is only installed when Visual Studio is installed on the machine.  Therefore, short of needing to install MVC and Visual Studio on a production environment, we need to deploy the libraries with out application - and we'd like to do so automatically.

There are a few ways to automatically deploy the 'Microsoft.Web.Infrastructure.dll' component library with your application.  The steps depend on which version of Visual Studio you are using.

Read more

How to Programmatically Impersonate Users in SharePoint

There are times when developing custom web parts and other SharePoint solutions, you need to impersonate a user and view SharePoint from within their context.

Impersonating a user in SharePoint requires a few things:

  • the account that the web or console application users that has privileges to impersonate other users (typically this would be the system account)
  • specific users' user tokens

Read more

Testing LINQ Statements Using LINQPad

The other day I was needing a way to test some LINQ statements from an application which rebuilds a database while performing some calculations.  The rebuilding process is just shy of 24 hours.

Besides, there have been numerous other times in which I needed to test LINQ statements.

Troubleshooting LINQ can be difficult at times due to it deferred execution - the statement is executed as the last possible moment.  I wanted a tool similar to SQL Management Studio in which I could run LINQ queries against a database.  Thus, I stumbled upon (actually, it wasn't very hard to find) LINQPad.

Since, finding it, LINQPad has allowed me to test my queries prior to implementation in the application.  Therefore, I can better predict the result instead of waiting through a long process and/or waiting for QA to troubleshoot.

LINQPad allows the user to run C#, VB.NET, F# and SQL statements/expressions against the database - very flexible and extremely helpful.  LINQPad's flexibility allows the connection's data context to be LINQ to SQL, Entity Framework and WCF (OData).

Take a look at LINQPad at