|
| 1 | +--- |
| 2 | +author: tdykstra |
| 3 | +ms.author: wpickett |
| 4 | +ms.date: 09-22-2025 |
| 5 | +--- |
| 6 | + |
| 7 | +# Copilot Code Instructions for `dotnet/AspNetCore.Docs` |
| 8 | + |
| 9 | +## Introduction |
| 10 | +This document contains code-specific instructions for GitHub Copilot when assisting with the `dotnet/AspNetCore.Docs` repository. For general instructions and documentation guidelines, please refer to the [copilot-instructions.md](./copilot-instructions.md) file. |
| 11 | + |
| 12 | +## Code-Related Guidelines |
| 13 | + |
| 14 | +### 1. Code Snippets |
| 15 | +- [ ] For code snippets longer than 6 lines: |
| 16 | + - [ ] Create a subfolder named after the document the snippet supports. |
| 17 | + - [ ] Create a `snippets` folder inside that subfolder. |
| 18 | + - [ ] For the code file: |
| 19 | + - [ ] If the snippet is not version-specific, place the code in a file with the appropriate extension (for example, `.cs` for C#) in the `snippets` folder. |
| 20 | + - [ ] If the snippet is version-specific: |
| 21 | + - [ ] Create a subfolder inside the `snippets` folder named for the version (for example, `9.0` for .NET 9 or ASP.NET Core 9). |
| 22 | + - [ ] Place the code in a file with the correct extension inside the version subfolder. |
| 23 | + - [ ] Add a project file (`.csproj`) to the version subfolder targeting the matching .NET version, if necessary to run or build the snippet. |
| 24 | +- [ ] Reference snippets using triple-colon syntax: |
| 25 | + - [ ] **Use file-relative paths** for snippets located in the same file as the articles that refer to it. |
| 26 | + ``` |
| 27 | + :::code language="csharp" source="../snippets/my-doc/Program.cs"::: |
| 28 | + ``` |
| 29 | + - [ ] **Use repository root-relative paths** for shared snippets: |
| 30 | + ``` |
| 31 | + :::code language="csharp" source="~/tutorials/min-web-api/samples/9.x/todoGroup/TodoDb.cs"::: |
| 32 | + ``` |
| 33 | +- [ ] For longer snippets, highlight specific lines: |
| 34 | + ``` |
| 35 | + :::code language="csharp" source="~/path/to/file.cs" range="5-10" highlight="2-3"::: |
| 36 | + ``` |
| 37 | +- [ ] Use the latest, non-preview C# coding patterns in non-preview code examples |
| 38 | +- [ ] Use the latest preview C# coding patterns in preview code examples |
| 39 | +- [ ] Use the following language code and indentation standards for markdown code blocks or the `language` attribute of code snippets: |
| 40 | +
|
| 41 | + Content of the snippet | Language code | Indentation in spaces |
| 42 | + :--------------------: | :-----------: | :-------------------: |
| 43 | + C# | csharp | 4 |
| 44 | + HTML | html | 4 |
| 45 | + CSS | css | 4 |
| 46 | + JavaScript | javascript | 2 spaces (use 4 spaces for line continuation) |
| 47 | + XML | xml | 2 |
| 48 | + JSON | json | 2 |
| 49 | + Console | console | 2 |
| 50 | + Text | - | 2 |
| 51 | +
|
| 52 | +- [ ] Code Snippet Folder Structure Requirements: |
| 53 | + - [ ] For code snippets longer than 6 lines, create proper folder structure: article-name/snippets/version/filename.cs (e.g., cookie/snippets/6.0/Program.cs) |
| 54 | + - [ ] Create version-specific subfolders: 3.x, 6.0, 8.0, 9.0, etc. |
| 55 | + - [ ] Use file-relative paths for snippets in same article folder |
| 56 | + - [ ] Use repository root-relative paths (~/) for shared snippets |
| 57 | +- [ ] Code Snippet Markers in .cs Files - CRITICAL: |
| 58 | + - [ ] NEVER use #region snippet_name and #endregion syntax in .cs files |
| 59 | + - [ ] ALWAYS use // <snippet_name> and // </snippet_name> format (all lowercase) |
| 60 | + - [ ] Examples of correct .cs file snippet markers: |
| 61 | + ```csharp |
| 62 | + // <snippet_policy> |
| 63 | + var cookiePolicyOptions = new CookiePolicyOptions |
| 64 | + { |
| 65 | + MinimumSameSitePolicy = SameSiteMode.Strict, |
| 66 | + }; |
| 67 | + app.UseCookiePolicy(cookiePolicyOptions); |
| 68 | + // </snippet_policy> |
| 69 | + ``` |
| 70 | + - [ ] INCORRECT format to avoid: |
| 71 | + ```csharp |
| 72 | + #region snippet_policy |
| 73 | + // code here |
| 74 | + #endregion |
| 75 | + ``` |
| 76 | +- [ ] Code Comments and Localization: |
| 77 | + - [ ] NEVER add explanatory code comments like `// Configure cookie policy options` in .cs snippet files |
| 78 | + - [ ] NEVER add comments like `// Add Cookie Policy Middleware` - these prevent proper localization |
| 79 | + - [ ] Rely on markdown prose before/after code snippets for explanations instead of inline comments |
| 80 | + - [ ] Only keep comments that are essential to the code's functionality |
| 81 | +- [ ] Common Syntax Errors to Avoid: |
| 82 | + - [ ] Using `range="5-10"` instead of `id="snippet_name"` |
| 83 | + - [ ] Using `name="snippet_name"` instead of `id="snippet_name"` |
| 84 | + - [ ] Mixing old [!code-csharp[]] syntax with new triple-colon syntax. Use triple-colon syntax. |
| 85 | + - [ ] Using absolute line numbers in highlight="" instead of relative to snippet |
| 86 | + - [ ] Using #region/#endregion in .cs files instead of // <snippet_name> format |
| 87 | +- [ ] Version-Specific Considerations: |
| 88 | + - [ ] Create separate snippet files for different .NET versions (3.x, 6.0, 8.0, 9.0+) |
| 89 | + - [ ] Ensure examples use appropriate syntax for the target version |
| 90 | + - [ ] Reference the correct version-specific snippet file in markdown |
| 91 | +
|
| 92 | +### 2. Code Build and Testing Requirements |
| 93 | +- [ ] Ensure all code samples build successfully against the targeted .NET version |
| 94 | +- [ ] Include necessary using statements in code samples |
| 95 | +- [ ] When you're assigned an issue involving code changes, after you've completed your work and the workflows (status checks) have run, ensure there are no build warnings under the OpenPublishing.Build status check |
| 96 | +- [ ] Use `#nullable enable` in C# code samples that use nullable reference types |
| 97 | +- [ ] For Minimal API examples, use the latest patterns including group-based routing where appropriate |
| 98 | +- [ ] For code samples targeting preview versions: |
| 99 | + - [ ] Clearly indicate in comments or surrounding documentation that the code targets a preview version |
| 100 | + - [ ] Provide fallback examples for current stable versions when possible |
| 101 | +
|
| 102 | +### 3. ASP.NET Core Code-Specific Guidelines |
| 103 | +- [ ] Use the latest supported version for examples unless otherwise specified |
| 104 | +- [ ] Include differences between Minimal API and controller-based approaches when relevant |
| 105 | +- [ ] For middleware content and examples, use the middleware class approach |
| 106 | +- [ ] Code examples should be concise and focused on demonstrating a specific concept |
| 107 | +- [ ] Include error handling in code examples where appropriate |
| 108 | +- [ ] Ensure all code is accessible and follows best practices for ASP.NET Core applications |
0 commit comments