-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Update file-based apps reference article #50604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
meaghanlewis
merged 7 commits into
dotnet:main
from
meaghanlewis:update-file-based-apps
Dec 18, 2025
+44
−20
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af99584
update article based on review feedback
meaghanlewis b02a09e
edit pass
meaghanlewis 4214105
fix review feedback
meaghanlewis aef8abc
clarify path output details
meaghanlewis cd91d75
add period to end of sentence
meaghanlewis 2fa0147
edit pass
meaghanlewis 62d4c61
Add note about dotnet user-secrets list command
meaghanlewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ Adds a NuGet package reference to your application. | |
|
|
||
| ```csharp | ||
| #:package Newtonsoft.Json | ||
| #:package Serilog version="3.1.1" | ||
| #:package Serilog@3.1.1 | ||
| ``` | ||
|
|
||
| ### `#:project` | ||
|
|
@@ -55,6 +55,7 @@ Specifies the SDK to use. Defaults to `Microsoft.NET.Sdk`. | |
|
|
||
| ```csharp | ||
| #:sdk Microsoft.NET.Sdk.Web | ||
| #:sdk [email protected] | ||
| ``` | ||
|
|
||
| ## CLI commands | ||
|
|
@@ -63,7 +64,13 @@ The .NET CLI provides full support for file-based apps through familiar commands | |
|
|
||
| ### Run applications | ||
|
|
||
| Run a file-based app directly by using the `dotnet run` command: | ||
| Run a file-based app by using the `dotnet run` command with the `--file` option: | ||
|
|
||
| ```dotnetcli | ||
| dotnet run --file file.cs | ||
| ``` | ||
|
|
||
| Or use the `dotnet run` command followed by the name of the file: | ||
|
|
||
| ```dotnetcli | ||
| dotnet run file.cs | ||
|
|
@@ -77,24 +84,18 @@ dotnet file.cs | |
|
|
||
| #### Pass arguments | ||
|
|
||
| Pass arguments to your application in several ways: | ||
| Pass arguments to your application by placing them after `--`: | ||
|
|
||
| ```dotnetcli | ||
| dotnet run file.cs -- arg1 arg2 | ||
| ``` | ||
|
|
||
| Arguments after `--` are passed to your application. Without `--`, arguments go to the `dotnet run` command: | ||
| Without `--`, arguments go to the `dotnet run` command: | ||
|
|
||
| ```dotnetcli | ||
| dotnet run file.cs arg1 arg2 | ||
| ``` | ||
|
|
||
| However, with the shorthand syntax, all arguments go to your application: | ||
|
|
||
| ```dotnetcli | ||
| dotnet file.cs arg1 arg2 | ||
| ``` | ||
|
|
||
| ### Build applications | ||
|
|
||
| Compile your file-based app by using the `dotnet build` command: | ||
|
|
@@ -103,7 +104,9 @@ Compile your file-based app by using the `dotnet build` command: | |
| dotnet build file.cs | ||
| ``` | ||
|
|
||
| The SDK generates a temporary project and builds your application. | ||
| The SDK generates a virtual project and builds your application. By default, the build output goes to the system's temporary directory under `<temp>/dotnet/runfile/<appname>-<appfilesha>/bin/<configuration>/`. | ||
|
|
||
| Use the `--output` option with the `dotnet build` command to specify a different path. To define a new default output path, set the `OutputPath` property at the top of your file by using the directive: `#:property OutputPath=./output`. | ||
|
|
||
| ### Clean build outputs | ||
|
|
||
|
|
@@ -113,21 +116,25 @@ Remove build artifacts by using the `dotnet clean` command: | |
| dotnet clean file.cs | ||
| ``` | ||
|
|
||
| Clean all file-based apps in a directory: | ||
| Delete cache for file-based apps in a directory: | ||
|
|
||
| ```dotnetcli | ||
| dotnet clean file-based-apps | ||
| ``` | ||
|
|
||
| Use the `--days` option with the preceding command to specify how many days an artifact folder needs to be unused before removal. The default number of days is 30. | ||
|
|
||
| ### Publish applications | ||
|
|
||
| Create a deployment package by using the `dotnet publish` command: | ||
| File-based apps enable native AOT publishing by default, producing optimized, self-contained executables. Disable this feature by adding `#:property PublishAot=false` at the top of your file. | ||
|
|
||
| Use the `dotnet publish` command to create an independent executable: | ||
|
|
||
| ```dotnetcli | ||
| dotnet publish file.cs | ||
| ``` | ||
|
|
||
| File-based apps enable native AOT publishing by default, producing optimized, self-contained executables. | ||
| The default location of the executable is an `artifacts` directory next to the `.cs` file, with a subdirectory named after the application. Use the `--output` option with the `dotnet publish` command to specify a different path. | ||
|
|
||
| ### Package as tool | ||
|
|
||
|
|
@@ -137,7 +144,7 @@ Package your file-based app as a .NET tool by using the `dotnet pack` command: | |
| dotnet pack file.cs | ||
| ``` | ||
|
|
||
| File-based apps set `PackAsTool=true` by default. | ||
| File-based apps set `PackAsTool=true` by default. Disable this setting by adding `#:property PackAsTool=false` at the top of your file. | ||
|
|
||
| ### Convert to project | ||
|
|
||
|
|
@@ -147,7 +154,7 @@ Convert your file-based app to a traditional project by using the `dotnet projec | |
| dotnet project convert file.cs | ||
| ``` | ||
|
|
||
| This command creates a `.csproj` file with equivalent SDK and properties. All `#` directives are removed from the `.cs` file and turned into elements in the corresponding `.csproj` file. | ||
| This command makes a copy of the `.cs` file and creates a `.csproj` file with equivalent SDK items, properties, and package references based on the original file's `#:` directives. Both files are placed in a directory named for the application next to the original `.cs` file, which is left untouched. | ||
|
|
||
| ### Restore dependencies | ||
|
|
||
|
|
@@ -157,7 +164,7 @@ Restore NuGet packages referenced in your file by using the `dotnet restore` com | |
| dotnet restore file.cs | ||
| ``` | ||
|
|
||
| Restore runs implicitly when you build or run your application. | ||
| By default, restore runs implicitly when you build or run your application. However, you can pass `--no-restore` to both the `dotnet build` and `dotnet run` commands to build or run without implicitly restoring. | ||
|
|
||
| ## Default included items | ||
|
|
||
|
|
@@ -192,9 +199,17 @@ File-based apps generate a stable user secrets ID based on a hash of the full fi | |
| Access user secrets the same way as traditional projects: | ||
|
|
||
| ```dotnetcli | ||
| dotnet user-secrets set "ApiKey" "your-secret-value" --project file.cs | ||
| dotnet user-secrets set "ApiKey" "your-secret-value" --file file.cs | ||
| ``` | ||
|
|
||
| List user secrets for file-based apps: | ||
meaghanlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```dotnetcli | ||
| dotnet user-secrets list --file file.cs | ||
| ``` | ||
|
|
||
| The `dotnet user-secrets list` command prints the value of your secrets. Don't put this command in scripts that run in public contexts. | ||
|
|
||
| For more information, see [Safe storage of app secrets in development](/aspnet/core/security/app-secrets). | ||
|
|
||
| ## Launch profiles | ||
|
|
@@ -287,6 +302,9 @@ Run directly: | |
| ./file.cs | ||
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > Use `LF` line endings instead of `CRLF` when you add a shebang. Don't include a BOM in the file. | ||
|
|
||
| ## Implicit build files | ||
|
|
||
| File-based apps respect MSBuild and NuGet configuration files in the same directory or parent directories. These files affect how the SDK builds your application. Be mindful of these files when organizing your file-based apps. | ||
|
|
@@ -313,7 +331,7 @@ Specifies the .NET SDK version to use. File-based apps respect this version sele | |
|
|
||
| ## Build caching | ||
|
|
||
| The .NET SDK caches build outputs to improve performance on subsequent builds. File-based apps participate in this caching system. | ||
| The .NET SDK caches build outputs to improve performance on subsequent invocations of `dotnet run`. This caching system is unique to file-based apps. | ||
|
|
||
| ### Cache behavior | ||
|
|
||
|
|
@@ -322,7 +340,7 @@ The SDK caches build outputs based on: | |
| - Source file content. | ||
| - Directive configuration. | ||
| - SDK version. | ||
| - Implicit build files. | ||
| - Implicit build file existence and content. | ||
|
|
||
| Caching improves build performance but can cause confusion when: | ||
|
|
||
|
|
@@ -331,6 +349,12 @@ Caching improves build performance but can cause confusion when: | |
|
|
||
| ### Workarounds | ||
|
|
||
| - Clear cache artifacts for file-based apps by using the following command: | ||
|
|
||
| ```dotnetcli | ||
| dotnet clean file-based-apps | ||
| ``` | ||
|
|
||
| - Run a full build by using the `--no-cache` flag: | ||
|
|
||
| ```dotnetcli | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.