Thursday, August 18, 2011

creating custom short cut snippet in visual studio for MVC

Someone you may know about short cuts in visual studio but do you know how to create your own?

you can use "ctor" and tab tab to get this
public HomeController()
{
}

"prop" and tab tab for
public int MyProperty { get; set; }

but you can create one like "view" to create this
public ActionResult foobar()
{
return View();
}


This is how
1. go here C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C# (for visual studio 9)
2. copy of those file and change the name to like "view.snippet" and update the xml file.


<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">

<Header>

<Title>view</Title>

<Shortcut>view</Shortcut>

<Description>Code snippet for an automatically implemented property</Description>

<Author>Wil</Author>

<SnippetTypes>

<SnippetType>Expansion</SnippetType>

</SnippetTypes>

</Header>

<Snippet>

<Declarations>

<Literal>

<ID>name</ID>

<ToolTip>View Name</ToolTip>

<Default>foobar</Default>

</Literal>

</Declarations>

<Code Language="csharp"><![CDATA[public ViewResult $name$()

{

return View();

}]]>

</Code>

</Snippet>

</CodeSnippet>

</CodeSnippets>


Now you type in "view" and tab tab in the HomeController or whatever else and you shoudl get this
public ActionResult foobar()
{
return View();
}


that's all.

more info here http://msdn.microsoft.com/en-us/library/ms165394.aspx

and I used http://www.simplebits.com/cgi-bin/simplecode.pl to convert xml code so that that blogger will allow me to enter the xml code.

No comments:

Post a Comment