Skip to content

Fix DNS-invalid hostnames with underscores in dotnet new templates#64988

Merged
DamianEdwards merged 10 commits intomainfrom
copilot/fix-dns-names-underscore-issue
Jan 10, 2026
Merged

Fix DNS-invalid hostnames with underscores in dotnet new templates#64988
DamianEdwards merged 10 commits intomainfrom
copilot/fix-dns-names-underscore-issue

Conversation

Copy link
Contributor

Copilot AI commented Jan 9, 2026

Fix DNS names with underscores in dotnet new templates

Summary:

Fixed issue where dotnet new templates generate DNS-invalid hostnames with underscores when project names contain dots (e.g., my.namespace.webmy_namespace_web), violating RFC 952/1123.

Changes Made:

  • Created custom value forms for DNS-safe hostname transformation
  • Added derived symbols that transform sourceName into RFC-compliant hostnames
  • Used distinct placeholder LocalhostTldHostNamePrefix for template replacement
  • Updated launchSettings.json files to use the new placeholder
  • Added support for passing project name via -n parameter in test infrastructure
  • Fixed -n parameter to avoid conflicts when args already contains -n or --name
  • Added comprehensive parameterized tests for EmptyWeb and BlazorWeb templates
  • Refactored tests to use Theory with InlineData for better maintainability
  • Refactored VerifyDnsCompliantHostname to shared Project class to eliminate duplication
  • Tested with various edge cases including numbers in project names

Technical Implementation:

Templates (EmptyWeb & BlazorWeb):

  1. Created lowerCaseInvariantWithHyphens value form chain that:

    • Lowercases the name
    • Replaces all invalid DNS chars with hyphens
    • Removes leading/trailing hyphens
  2. Added hostName derived symbol that applies the transform and replaces LocalhostTldHostNamePrefix placeholder

  3. Updated launchSettings.json to use the distinct placeholder

Test Infrastructure:

  1. Modified Project.cs to:
    • Pass -n parameter when ProjectName is set
    • Check if args already contains -n or --name to avoid conflicts
    • Added VerifyDnsCompliantHostname method for shared use across test projects
  2. Added overload to ProjectFactoryFixture.CreateProject accepting a project name
  3. Added LocalhostTld constant to ArgConstants

Tests Added (EmptyWeb & BlazorWeb):

  • Refactored to use ConditionalTheory with InlineData for parameterized testing
  • Single test method *TemplateLocalhostTld_GeneratesDnsCompliantHostnames covers all scenarios:
    • my.namespace.*my-namespace-* (dots to hyphens)
    • .StartWithDotstartwithdot (no leading hyphen)
    • EndWithDot.endwithdot (no trailing hyphen)
    • My..Test__Projectmy--test--project (multiple invalid chars)
    • Project123.Test456project123-test456 (numbers preserved per RFC 1123)

Refactoring:

  • Moved VerifyDnsCompliantHostname from individual test classes to shared Project class
  • Consolidated 5 separate test methods into 1 parameterized test per template
  • Improved test maintainability by making it easier to add new test cases
  • Eliminated code duplication between EmptyWeb and BlazorWeb test classes

Compliance:

✅ RFC 952: Hostnames use letters, digits, and hyphens only
✅ RFC 1123: Allows starting with digits, no underscores
✅ No leading/trailing hyphens in generated hostnames

Original prompt

This section details on the original issue you should resolve

<issue_title>DNS names generated with underscores (_) violate RFC 952/1123 hostname rules during dotnet new</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The template/tooling generates DNS names (e.g., hostnames or domain labels) that contain underscores (_). According to DNS hostname specifications (RFC 952 and RFC 1123), underscores are not valid characters in hostnames. This causes failures in environments and libraries that strictly validate DNS names (e.g., Kubernetes, Docker, cloud DNS, and DNS client libraries).

Actual Behavior

Generated names include underscores—e.g., my_service.example.com, MY_SERVICE, or derived labels such as my_service-api. These are rejected by systems enforcing RFC-compliant DNS hostnames.

Impact

  • Deployment failures in Kubernetes/Docker due to invalid DNS labels.
  • DNS resolution errors in client libraries.
  • Operational inconsistencies if environments silently mutate invalid names.
  • Developer confusion when template output contradicts DNS standards.

References

  • RFC 952 — DoD Internet Host Table Specification: hostnames use letters, digits, and hyphen; no leading/trailing hyphen.
  • RFC 1123 — Requirements for Internet Hosts – Application and Support: relaxes RFC 952 to allow starting with a digit; underscores remain invalid for hostnames.
  • SRV records (underscores in service labels are valid): see service naming like _sip._tcp.example.com.

Additional Notes

If this template also generates SRV/DKIM/DMARC records or service identifiers, underscores are expected only in those service labels—not in hostnames.

Expected Behavior

Generated names should only use:

  • Letters a–z (case-insensitive)
  • Digits 0–9
  • Hyphen -

Additional rules:

  • Labels must not start or end with a hyphen.
  • Each label must be ≤ 63 characters.
  • The full domain must be ≤ 253 characters.
  • No underscores in hostnames (allowed only in special record labels like SRV/DKIM/DMARC service identifiers, e.g., _sip._tcp).

Steps To Reproduce

Create a new project using localhost tld's and a name including dots (.)

dotnet new web --localhost-tld -n "my.namespace.web"

Exceptions (if any)

No response

.NET Version

10.0.101

Anything else?

No response</issue_description>

<agent_instructions>This is due to these lines in the two effected templates:

These refer to the template's sourceName value using the "lower case class name" default naming form. This naming form lowercases the value and replaces any characters invalid for use in a C# class name with an underscore.

To fix this the effected templates will need to define custom value forms that are then used by a declared derived symbol that transforms the sourceName into a value safe for use in a host name, then the launchSettings.json files can be updated to use the value declared in the symbol's replaces value.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Jan 9, 2026
Copilot AI changed the title [WIP] Fix DNS names generated with underscores in dotnet new Fix DNS-invalid hostnames with underscores in dotnet new templates Jan 9, 2026
Copilot AI requested a review from DamianEdwards January 9, 2026 02:33
@DamianEdwards DamianEdwards marked this pull request as ready for review January 9, 2026 06:11
Copilot AI review requested due to automatic review settings January 9, 2026 06:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes DNS-invalid hostname generation in ASP.NET Core templates by replacing underscores with hyphens to comply with RFC 952/1123 specifications when project names contain dots or other invalid DNS characters.

Key Changes:

  • Added custom value form chain (lowerCaseInvariantWithHyphens) that transforms project names into DNS-compliant hostnames
  • Created hostName derived symbol that applies the transformation and replaces the LocalhostTldHostNamePrefix placeholder
  • Updated launchSettings.json files to use the new placeholder instead of hardcoded lowercase class names

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/ProjectTemplates/Web.ProjectTemplates/content/EmptyWeb-CSharp/.template.config/template.json Added custom value forms and hostName symbol to transform project names into DNS-compliant hostnames
src/ProjectTemplates/Web.ProjectTemplates/content/EmptyWeb-CSharp/Properties/launchSettings.json Replaced hardcoded lowercase hostname with LocalhostTldHostNamePrefix placeholder for both HTTP and HTTPS profiles
src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json Added custom value forms and hostName symbol to transform project names into DNS-compliant hostnames
src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Properties/launchSettings.json Replaced hardcoded lowercase hostname with LocalhostTldHostNamePrefix placeholder for both HTTP and HTTPS profiles

@DamianEdwards DamianEdwards merged commit 5fc39b0 into main Jan 10, 2026
25 checks passed
@DamianEdwards DamianEdwards deleted the copilot/fix-dns-names-underscore-issue branch January 10, 2026 21:45
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Jan 10, 2026
DamianEdwards added a commit that referenced this pull request Jan 13, 2026
…64988)

* Initial plan

* Add DNS-safe hostname forms and symbols to templates

Co-authored-by: DamianEdwards <[email protected]>

* Add trimming of leading/trailing hyphens for DNS-safe hostnames

Co-authored-by: DamianEdwards <[email protected]>

* Use distinct placeholder LocalhostTldHostNamePrefix for hostname replacement

Co-authored-by: DamianEdwards <[email protected]>

* Add tests for DNS-compliant hostname generation with --localhost-tld

Co-authored-by: DamianEdwards <[email protected]>

* Add tests for DNS-compliant hostname generation in Blazor templates

Co-authored-by: DamianEdwards <[email protected]>

* Move VerifyDnsCompliantHostname to shared Project class

Co-authored-by: DamianEdwards <[email protected]>

* Fix -n parameter conflict and add tests with numbers in project names

Co-authored-by: DamianEdwards <[email protected]>

* Refactor DNS hostname tests to use Theory with InlineData

Co-authored-by: DamianEdwards <[email protected]>

* Fix razorcomponent item test

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: DamianEdwards <[email protected]>
Co-authored-by: Damian Edwards <[email protected]>
wtgodbe pushed a commit that referenced this pull request Jan 14, 2026
…stnames in launch profiles (#65048)

* Fix DNS-invalid hostnames with underscores in dotnet new templates (#64988)

* Initial plan

* Add DNS-safe hostname forms and symbols to templates

Co-authored-by: DamianEdwards <[email protected]>

* Add trimming of leading/trailing hyphens for DNS-safe hostnames

Co-authored-by: DamianEdwards <[email protected]>

* Use distinct placeholder LocalhostTldHostNamePrefix for hostname replacement

Co-authored-by: DamianEdwards <[email protected]>

* Add tests for DNS-compliant hostname generation with --localhost-tld

Co-authored-by: DamianEdwards <[email protected]>

* Add tests for DNS-compliant hostname generation in Blazor templates

Co-authored-by: DamianEdwards <[email protected]>

* Move VerifyDnsCompliantHostname to shared Project class

Co-authored-by: DamianEdwards <[email protected]>

* Fix -n parameter conflict and add tests with numbers in project names

Co-authored-by: DamianEdwards <[email protected]>

* Refactor DNS hostname tests to use Theory with InlineData

Co-authored-by: DamianEdwards <[email protected]>

* Fix razorcomponent item test

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: DamianEdwards <[email protected]>
Co-authored-by: Damian Edwards <[email protected]>

* Add replaceRepeatedHyphens step to template processing and update tests for DNS-compliant hostnames (#65027)

Related to #64978

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: DamianEdwards <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DNS names generated with underscores (_) violate RFC 952/1123 hostname rules during dotnet new

4 participants