|
| 1 | +--- |
| 2 | +no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] |
| 3 | +--- |
| 4 | +Collocation of JavaScript (JS) files for pages, views, and Razor components is a convenient way to organize scripts in an app. |
| 5 | + |
| 6 | +Collocate JS files using the following filename extension conventions: |
| 7 | + |
| 8 | +* Pages of Razor Pages apps and views of MVC apps: `.cshtml.js`. Examples: |
| 9 | + * `Pages/Contact.cshtml.js` for the `Contact` page of a Razor Pages app at `Pages/Contact.cshtml`. |
| 10 | + * `Views/Home/Contact.cshtml.js` for the `Contact` view of an MVC app at `Views/Home/Contact.cshtml`. |
| 11 | +* Razor components of Blazor apps: `.razor.js`. Example: `Pages/Index.razor.js` for the `Index` component at `Pages/Index.razor`. |
| 12 | + |
| 13 | +Collocated JS files are publicly addressable using the path to the file in the project: |
| 14 | + |
| 15 | +* Pages, views, and components from a collocated scripts file in the app: |
| 16 | + |
| 17 | + `{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}` |
| 18 | + |
| 19 | + * The `{PATH}` placeholder is the path to the page, view, or component. |
| 20 | + * The `{PAGE, VIEW, OR COMPONENT}` placeholder is the page, view, or component. |
| 21 | + * The `{EXTENSION}` placeholder matches the extension of the page, view, or component, either `razor` or `cshtml`, followed by `.js`. |
| 22 | + |
| 23 | + In the following example from a Razor Pages app, the script is collocated in the `Pages` folder with the `Contact` page (`Pages/Contact.cshtml`): |
| 24 | + |
| 25 | + ```razor |
| 26 | + @section Scripts { |
| 27 | + <script src="~/Pages/Contact.cshtml.js"></script> |
| 28 | + } |
| 29 | + ``` |
| 30 | + |
| 31 | +* For scripts provided by a Razor class library (RCL): |
| 32 | + |
| 33 | + `_content/{PACKAGE ID}/{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}` |
| 34 | + |
| 35 | + * The `{PACKAGE ID}` placeholder is the RCL's package identifier (or library name for a class library referenced by the app). |
| 36 | + * The `{PATH}` placeholder is the path to the page, view, or component. If a Razor component is located at the root of the RCL, the path segment isn't included. |
| 37 | + * The `{PAGE, VIEW, OR COMPONENT}` placeholder is the page, view, or component. |
| 38 | + * The `{EXTENSION}` placeholder matches the extension of page, view, or component, either `razor` or `cshtml`, followed by `.js`. |
| 39 | + |
| 40 | + In the following Blazor app example: |
| 41 | + |
| 42 | + * The RCL's package identifier is `AppJS`. |
| 43 | + * A module's scripts are loaded for the `Index` component (`Index.razor`). |
| 44 | + * The `Index` component is in the `Pages` folder of the RCL. |
| 45 | + |
| 46 | + ```csharp |
| 47 | + var module = await JS.InvokeAsync<IJSObjectReference>("import", |
| 48 | + "_content/AppJS/Pages/Index.razor.js"); |
| 49 | + ``` |
0 commit comments