There are only about 10-15 snippets that are currently shipping with MVC (and many of those are just duplicated because there are both VB and C# versions). That doesn’t seem like very many. But let’s have a look at one that would be pretty common – the HTML helper for a text box:
That snippet produces the following code:
1: <%= Html.TextBox("FirstName") %>
Hmmm. That code doesn’t seem very “MVC2ish”. First off, it’s using the old school double-quoted string to specify the property rather than the new strongly-typed lambda-based HTML helper. Secondly, it’s using the TextBox() method rather than the EditorFor() method. Third, it’s using the old <%= syntax which Microsoft is trying to get us to forget exists, rather than the new <%: HTML encoding syntax.
The best approach here is for us to create our own snippets customized to our heart’s content. But the snippet files are not the easiest things to work with so rather than starting from scratch, let’s just grab the MVC textbox snippet and modify it. The snippets can be found in the directory: C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 2\Visual Studio 2010\Snippets\HTML\1033\ASP.NET MVC 2. I’m going to grab the “textboxmvc.snippet” file from there, create a directory called “My MVC2 Snippets” and put it inside of the normal code snippets directory structure for Visual Studio. So the full path will be: <My Documents>\Visual Studio 2010\Code Snippets\Visual Web Developer\My MVC2 Snippets. Then I’ll rename “textboxmvc.snippet” to “editorfor.snippet”.
In order to get Visual Studio to recognize this location, I need to do Tools –> Code Snippet Manager and select the directory:
Next I’ll open the editorfor.snippet file in any code editor and change a few key lines:
1: <CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
4: <Author>Steve Michelotti</Author>
6: <Description>Markup snippet for an ASP.NET MVC EditorFor helper</Description>
20: <Code Language="html" Key="*"><![CDATA[<%: Html.EditorFor(m => m$property$) %>$end$]]></Code>
Line 3, 4, 5, and 6 are simple and straight-forward replacements. The primary line I need to change is line 20. I’m going to use the generic “m” (for “Model”) for my lambda identifier and then use a replacement variable called $property$ (which I also specify on lines 15-17). I intentionally left out the “.” because after the snippet expands, I want the next keystroke I press to be the “.” so that intellisense will naturally come up for me (this snippet can be expanded without the mouse by typing “<edi”):
Just hit <Enter> and you’re all done!
Snippets are an area that you should absolutely leverage to take care of your most common mundane coding tasks and customize them per your own personal preferences/standards. Historically, my tool of choice for editing/creating snippet files is Snippy (though this doesn’t appear to actively be under development any more). I’ve also used Snippet Designer which has nice integration with Visual Studio. It appears that neither of these tools (yet) have support for the new language=”html” that is new in VS2010 (hopefully will support soon).