Sep 15 2012

SpecsFor.Mvc 2.0 Released

Category: SpecsFor | MVCMatt @ 14:41

SpecsFor.Mvc 2.0 is out today.  This release includes drastically improved performance, support for Chrome, and support for the very latest versions of Selenium, ASP.NET MVC, and other libraries.

More...

Tags: , ,

May 19 2012

Using SpecsFor.Mvc - Establishing Context with Seed Data

Category: SpecsFor | Testing | ASP.NET | MVCMatt @ 16:11

SpecsFor.Mvc gives you a lot in a single NuGet package.  You get a test web host, a strongly-typed API for navigating around and interacting with your MVC app, and standard hooks for dealing with cross-cutting concerns like authentication, but you also get hooks that you can use to add your own behavior.  In this post, I’ll show you how to use one of those hooks to load seed data into your application to facilitate testing.

More...

Tags:

Apr 15 2012

Introducing MvcDisplayTemplates

Category: ASP.NET | MVC | SpecsForMatt @ 16:00

Beginning with version 2.0, ASP.NET MVC has shipped with a set of templates for both displaying and editing data.  These templates are buried within the System.Web.Mvc assembly.  While you can override them outright, you cannot easily extend them since they’re locked down.  The new MvcDisplayTemplates NuGet package fixes this problems.

More...

Tags:

Mar 30 2012

SpecsFor.Mvc 1.1 Released

Category: ASP.NET | MVC | Testing | SpecsForMatt @ 14:25

A new release of SpecsFor.Mvc is now live on NuGet.  This release includes one simple, but important, enhancement as well as a few other minor improvements.

More...

Tags:

Mar 8 2012

SpecsFor.Mvc 1.0 Released!

Category: SpecsFor | Testing | ASP.NET | MVCMatt @ 17:05

image

It’s late, so this will be a short post, but SpecsFor.Mvc version 1.0 has been released.  It took me a lot longer to wrap this up than I expected, but there are a lot more features in the 1.0 release than I had originally planned.  Overall, I’m very pleased with the end-user experience of the framework, though admittedly the internals of the project are in need of some cleanup and refactoring.  I have a lot of content planned that will highlight what SpecsFor.Mvc can do, but for now, this short video will have to do.  Be sure to watch the video in HD!  Enjoy!

Tags:

Mar 8 2012

ASP.NET MVC 3, Razor-C#, and VB.NET WebForms - ActionLink, RenderPartial, and RenderAction in WebForms

Category: ASP.NET | MVCMatt @ 15:55

Today I’m going to show you how to use MVC helper methods in your ASP.NET WebForms markup.  Why would you want to do this?  As I explained and demonstrated in the previous two posts in this series, I’m working on a project that has an extensive investment in WebForms (of the Visual Basic .NET variety), and a wholesale migration to MVC is just not possible.  With a little bit of black magic, I’ve shown you how to write C# MVC code and Razor views that can be consumed via a VB.NET WebForms app.  With this last “spell,” I’ll show you how you can start bringing that content into WebForms pages.

More...

Tags:

Mar 1 2012

ASP.NET MVC 3, Razor-C#, and VB.NET WebForms - Using Razor Views With WebForms Master Pages

Category: ASP.NET | MVCMatt @ 15:42

When we left off last time, I showed you how to write ASP.NET MVC code in C#, then consume and expose that functionality within a VB.NET WebForms project.  Why?  Because I’m maintaining a project with a huge investment in VB.NET WebForms, and a wholesale migration isn’t feasible.  This approach allows for a gradual migration from VB.NET WebForms to C# ASP.NET (with Razor!)  So far, all I’ve shown you how to do is render Razor views.  But what about all those existing VB.NET master pages?  Today I’ll show you how you can use these master pages as “layouts” for your Razor views.

More...

Tags:

Feb 15 2012

ASP.NET MVC 3, Razor-C#, and VB.NET WebForms - A Tale of Black Magic Voodoo

Category: ASP.NET | MVCMatt @ 15:45

What do you get when you mix a legacy VB.NET WebForms application, ASP.NET MVC, and Razor views that are written in C#?  If you said “Pain!” you are quite right.  But what you also get is the ability to leverage your existing investment in VB.NET WebForms while crafting new code in ASP.NET MVC Razory-C# goodness.  In this series of posts, I’m going to tell you how you, too, can concoct a wicked brew that will enable you to do crazy things, such as creating Razor views in C# that utilize VB.NET WebForms master pages, or how you can render MVC action methods from within WebForms markup.

More...

Tags:

Jul 7 2011

Cleaning up POSTs in ASP.NET MVC, the Fail Tracker Way

Category: Fail Tracker | MVC | ASP.NETMatt @ 12:36

Those who have worked with ASP.NET MVC for more than a day have no doubt found themselves repeating common patterns when handling POSTs.  Jimmy Bogard recently blogged one way to simplify your actions.  I handled the same problem in Fail Tracker by implementing a very simple convention (one-model-in, one-model-out) and pushing some responsibility into the application framework.  With this in place, cross-cutting POST handling logic can be pushed out of the action methods, and a common “doh” error (forgetting to perform server-side validation) can be eliminated.  Read on to find out how you can adopt this simple convention in your application framework. 

More...

Tags:

Jul 5 2011

Building Nice Display Names From Pascal-Case View Model Names in ASP.NET MVC 3

Category: ASP.NET | MVC | Fail TrackerMatt @ 12:56

One of my goals with Fail Tracker is to push the “Convention over Configuration” idea as far as you possibly can within the confines of ASP.NET MVC.  I’m obviously biased, but so far I think I’ve been quite successful, and Fail Tracker is probably the most enjoyable codebase I’ve ever worked with.  One convention I recently implemented eliminates the need to decorate view model properties with the DisplayNameAttribute.  The convention says “Pascal cased property names will be intelligently split into strings with spaces.”  Thanks to the infrastructure and pluggable model metadata system that Fail Tracker uses, implementing this convention was quite easy.

By default in ASP.NET MVC 3, a view model like the following:

public class LogOnForm
{
    [Required]
    [EmailAddress]
    public string EmailAddress { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
}

will generate a field label that looks like this:

image

Note the missing space between “Email” and “Address.”  This is obviously not what we want.  The way I solved it originally was to apply the DisplayNameAttribute to my view model:

public class LogOnForm
{
    [Required]
    [EmailAddress]
    [DisplayName("Email Address")]
    public string EmailAddress { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
}

which gives the desired UI:

image

But this seemed pointless to me.  Why should I have to remember to de-Pascal case my property names every time I created a view model?  Fortunately, I don’t have to.  Here’s the “model metadata filter” (a feature of the Fail Tracker application framework) that achieves the same thing:

public class PascalCaseToDisplayNameFilter : IModelMetadataFilter
{
    public void TransformMetadata(System.Web.Mvc.ModelMetadata metadata, IEnumerable<Attribute> attributes)
    {
        if (!string.IsNullOrEmpty(metadata.PropertyName) && !attributes.OfType<DisplayNameAttribute>().Any())
        {
            metadata.DisplayName = metadata.PropertyName.ToStringWithSpaces();
        }
    }
}

The ToStringWithSpaces extension method is a beautiful piece of Regex that James Kolpack gets credit for:

public static class StringExtensions
{
     public static string ToStringWithSpaces(this string input)
     {
         return Regex.Replace(
            input,
            "(?<!^)" + // don't match on the first character - never want to place a space here
            "(" +
            "  [A-Z][a-z] |" + // put a space before "Aaaa"
            "  (?<=[a-z])[A-Z] |" + // put a space into "aAAA" before the first capital
            "  (?<![A-Z])[A-Z]$" + // if the last letter is capital, prefix it with a space too
            ")",
            " $1",
            RegexOptions.IgnorePatternWhitespace);
     }
}

And that’s all there is to it.  Thanks to the application framework that is baked into Fail Tracker, simply creating the PascalCaseToDisplayNameFilter is all that’s necessary.  I can now remove most of my DisplayNameAttributes from my view models.  Do note though that the convention is quite easy to override.  If I do need to alter the labeling for a property, I can still use the DisplayNameAttribute exactly as before.  All my convention does is provide a more reasonable default without getting in the way of doing something different, which is exactly what a good convention should do.

If you want to know more about how these sorts of conventions are automagically wired up in Fail Tracker, feel free to check out Fail Tracker code.  I do plan to write much more about the application framework itself, but it may be a while as I’m targeting a magazine publication for that. Smile

Tags: