Custom Validation Errors in MVC

There are times where you may want to display custom validation error messages in MVC.  As for me, I'm not a huge fan of the classic ValidationSummary HTML helper.  I will still annotate my data model, but I want the errors to have a better presentation in the UI. Occasionally, I may want to display one error at a time.  In order to do all of this, there's a few steps that need to be implemented.

Read more


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:

@Html.ValidationSummary()

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=1.0.0.0, 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