Skip to content

Supporting multiple frameworks with code differences doesn't work #9576

@adrianm64

Description

@adrianm64

Product

dotnet CLI (dotnet new)

Describe The Bug

I'm trying to create a template that works with multiple frameworks but I can't get it to compile.

I have this template code

//#if (Framework == "net10.0")
app.MapOpenApi("/swagger/{documentName}.json");
//#elif (Framework == "net9.0")
app.MapOpenApi("/swagger/{documentName}.json");
//#else
app.UseSwagger(c => c.RouteTemplate = "/swagger/{documentName}.json");
//#endif

dotnet new generates the correct code from the template but I can't get the template to compile because UseSwagger and MapOpenApi are never available at the same time.

I tried to add #if NET10_0 etc for the compiler but they are processed by the template engine as well so the parts not matching the default framework was also removed in the generated project.

Also tried with #if NET10_0 || (Framework == net10) but it seems like any expression involving Framework == is true to the compiler (but not the template engine).

What I would like to do is something like

#if NET10_0 || Framework = "net10.0"
app.MapOpenApi("/swagger/{documentName}.json");
#endif
#if NET9_0 || Framework == "net9.0"
app.MapOpenApi("/swagger/{documentName}.json");
#endif
#if NET8_0 || Framework == "net8.0"
app.UseSwagger(c => c.RouteTemplate = "/swagger/{documentName}.json");
#endif

Is there another way to do this?

To Reproduce

Steps:

  1. In a template add this code
#if NET10_0
Console.WriteLine("NET == 10");
#else
Console.WriteLine("NET != 10");
#endif
  1. Expected result from dotnet new
Console.WriteLine("NET == 10");
Console.WriteLine("NET != 10");

or maybe unchanged

#if NET10_0
Console.WriteLine("NET == 10");
#else
Console.WriteLine("NET != 10");
#endif
  1. Actual result
Console.WriteLine("NET 10");

dotnet Info

output .NET SDK: Version: 10.0.100 Commit: b0f34d51fc Workload version: 10.0.100-manifests.5fb86115 MSBuild version: 18.0.2+b0f34d51f

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.100\

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
Version: 10.0.0
Architecture: x64
Commit: b0f34d51fc

.NET SDKs installed:
3.1.426 [C:\Program Files\dotnet\sdk]
10.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

Visual Studio Version

VS2026 18.0.1

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions