Skip to content

Instantly share code, notes, and snippets.

@ridercz
Created September 30, 2021 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ridercz/275062dc7eea7ce46b65320cf2443307 to your computer and use it in GitHub Desktop.
Save ridercz/275062dc7eea7ce46b65320cf2443307 to your computer and use it in GitHub Desktop.
Visual Studio Code Snippets
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Insert input model subclass</Title>
<Author>Michal A. Valasek</Author>
<Shortcut>imodel</Shortcut>
</Header>
<Snippet>
<Code Language="csharp" Delimiter="$"><![CDATA[[BindProperty]
public InputModel Input { get; set; } = new InputModel();
public class InputModel {
$end$
}]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Check if argument is null</Title>
<Author>Michal A. Valasek</Author>
<Shortcut>can</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>ArgumentName</ID>
<Default>arg</Default>
<ToolTip>Argument name</ToolTip>
</Literal>
</Declarations>
<Code Language="csharp" Delimiter="$"><![CDATA[if($ArgumentName$ == null) throw new ArgumentNullException(nameof($ArgumentName$));$end$]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Check if argument is null or empty string</Title>
<Shortcut>cane</Shortcut>
<Author>Michal A. Valasek</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>ArgumentName</ID>
<Default>arg</Default>
<ToolTip>Argument name</ToolTip>
</Literal>
</Declarations>
<Code Language="csharp" Delimiter="$"><![CDATA[if($ArgumentName$ == null) throw new ArgumentNullException(nameof($ArgumentName$));
if(string.IsNullOrEmpty($ArgumentName$)) throw new ArgumentException("Value cannot be empty string.", nameof($ArgumentName$));$end$]]>
</Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Check if argument is null, empty or whitespace string</Title>
<Shortcut>canew</Shortcut>
<Author>Michal A. Valasek</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>ArgumentName</ID>
<Default>arg</Default>
</Literal>
</Declarations>
<Code Language="csharp" Delimiter="$"><![CDATA[if($ArgumentName$ == null) throw new ArgumentNullException(nameof($ArgumentName$));
if(string.IsNullOrWhiteSpace($ArgumentName$)) throw new ArgumentException("Value cannot be empty or whitespace only string.", nameof($ArgumentName$));$end$]]>
</Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>If model state is not valid, return page.</Title>
<Shortcut>invr</Shortcut>
<Author>Michal A. Valasek</Author>
</Header>
<Snippet>
<Code Language="csharp" Delimiter="$"><![CDATA[if (!this.ModelState.IsValid) return this.Page();$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<SnippetType>Expansion</SnippetType>
<Title>Razor Form Paragraph</Title>
<Author>Michal A. Valášek</Author>
<Description>Markup snippet for a form paragraph in Razor.</Description>
<Shortcut>rfp</Shortcut>
<Kind>any</Kind>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>FieldName</ID>
<Default>Input.Property</Default>
</Literal>
</Declarations>
<Code Language="html" Delimiter="$"><![CDATA[<p>
<label asp-for="$FieldName$"></label><br />
<input asp-for="$FieldName$" />
</p>
$end$]]></Code>
</Snippet>
</CodeSnippet>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<SnippetType>Expansion</SnippetType>
<Title>Razor Form Table Row</Title>
<Author>Michal A. Valášek</Author>
<Description>Markup snippet for a form table row in Razor.</Description>
<Shortcut>rfr</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>FieldName</ID>
<Default>Input.Property</Default>
</Literal>
</Declarations>
<Code Language="html" Delimiter="$"><![CDATA[<tr>
<th><label asp-for="$FieldName$"></label></th>
<td><input asp-for="$FieldName$" /></td>
</tr>
$end$]]></Code>
</Snippet>
</CodeSnippet>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<SnippetType>Expansion</SnippetType>
<Title>Razor Form Table</Title>
<Author>Michal A. Valášek</Author>
<Description>Markup snippet for a form table in Razor.</Description>
<Shortcut>rft</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>ButtonLabel</ID>
<Default>OK</Default>
</Literal>
</Declarations>
<Code Language="html" Delimiter="$"><![CDATA[<table class="form">
<tbody>
$end$
</tbody>
<tfoot>
<tr>
<th></th>
<td>
<div asp-validation-summary="All"></div>
<input type="submit" value="$ButtonLabel$" />
</td>
</tr>
</tfoot>
</table>]]></Code>
</Snippet>
</CodeSnippet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment