4

Is it possible to add custom code snippets for Razor pages using the Code Snippets Manager in Visual Studio?

I can add snippets for HTML, but I can't find CSHTML anywhere.

I'm using Visual Studio Community 2019 16.4.1

3 Answers 3

4

That is because there are no dedicated Cshtml snippets. The Razor code is either C# or VB, so those snippets will be valid in a code block. To use those snippets you simply have to open a code-block:

  • Either type @ or @( to open an explicit statement
  • type foreach
  • type tab twice to activate the snippet

To edit the snippets, press Ctrl+K, Ctrl+B

Sign up to request clarification or add additional context in comments.

2 Comments

yes that worked. the only thing is that the inserted syntax from the snippet begins with @. My snippet creates a <!-- --> section, but now it results in @<!-- -->. is there a way to remove the @ sign or activate the snippets another way?
that snippet is an html snippet, isn't it? not C#. If I am not mistaken, you could use ZenCoding to insert html comments by pressing c --> tab.
2

This worked for me in Visual Studio 2019

Create and save your XML .snippet file. Example template:

<CodeSnippet Format="1.1.0">
<Header>
    <Title>NameOfSnippet</Title>
    <Author>YourName</Author>
    <Shortcut>ShortCutName</Shortcut>
    <Description>Description</Description>
    <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
</Header>
<Snippet>
    <Declarations>
        <Literal>
            <ID>Id</ID>
            <ToolTip>Please add an ID</ToolTip>
            <Default>REPLACE_ID</Default>
        </Literal>
    </Declarations>
    <Code Language="html"><![CDATA[<div id="$Id$">Example HTML Snippet</div>$end$]]></Code>
</Snippet>

  1. Tools > Code Snippet Manager > Language Dropdown: html > Import
  2. Navigation to your .snippet file and open
  3. Select both My HTML Snippets & HTML

enter image description here

  1. Select Finish and OK
  2. Open a cshtml file and type in your ShortCutName > hit tab

You should see your snippet in the cshtml, since language is html, and not CSharp, no need to open a code block

Comments

1

When I was trying to tackle this problem, the thing that was missing in my mental model was that a .cshtml file consists of multiple parts that are in their own language context. For example, you could have:

  • a <style></style> block that its contents would be css and VS would recognize CSS snippets in there,
  • a <script></script> block that has javascript content and VS would recognize javascript snippets
  • a @ block where vs could recognize either CSharp or Basic snippets
  • If you aren't in one of the other spots, html snippets are recognized.

In my case, I wanted to add a javascript snippet but I thought that the available snippets were tied directly with my file type so I was trying to add them as HTML snippets (and then was disappointed when they didn't show up in my script tag).

ALSO, its important to note that at least with VS 2017, you may need to restart VS to get the editor to recognize any new snippets.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.