Β© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#β€’8mo agoβ€’
30 replies
!!! Jasper !!!

Aspire custom connection string env variable

So i just started using aspire πŸš€
In my project i want to spin up a mongodb database, but my project uses a non default connection strings environment variable. The connection string should be in the variable
Database__ConnectionString
Database__ConnectionString
, but i can only get it to be the default
ConnectionString__X
ConnectionString__X
. Is there any way to "re-map" the default connectionstring varible to my custom one?

var mongodb = builder.AddMongoDB("mongodb", 27017)
    .WithMongoExpress(containerName: "express")
    .AddDatabase("database", "Development")
    ;

var api = builder.AddProject<Projects.BumpBuddy_Api>("api")
    .WithHttpHealthCheck("/health")
    .WithReference(mongodb);
var mongodb = builder.AddMongoDB("mongodb", 27017)
    .WithMongoExpress(containerName: "express")
    .AddDatabase("database", "Development")
    ;

var api = builder.AddProject<Projects.BumpBuddy_Api>("api")
    .WithHttpHealthCheck("/health")
    .WithReference(mongodb);



Edit:
Went to the aspire source code and got my answer

I added this to my api project:
.WithEnvironment("Database__ConnectionString", mongodb.Resource.ConnectionStringExpression)
.WithEnvironment("Database__ConnectionString", mongodb.Resource.ConnectionStringExpression)


mongodb.Resource.ConnectionStringExpression has the connectionstring.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

βœ… Database connection string
C#CC# / help
2y ago
OData filtering string variable
C#CC# / help
3y ago
Missing logs in Aspire after changing env and IDE
C#CC# / help
10mo ago
βœ… Storing connection string securely
C#CC# / help
2y ago