I have mvc project to deploy on commercial Windows web hosting, not Azure, no cloud services, no containers. I try to manage access to connectionstring. I put in into webconfig file:
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".RealProject.dll" stdoutLogEnabled="false" stdoutLogFile=".logsstdout" hostingModel="inprocess" />
</system.webServer>
</location>
<connectionStrings>
<add name="MyWebconfigConnString" connectionString="Data Source=RealServer;Database=RealName;Uid=RealUser;Password=RealPsw;Encrypt=Optional; TrustServerCertificate=True;" />
</connectionStrings>
</configuration>
The connection string itself works fine when accessed during development from appsettings.json. Now I want to access it from Program.cs to pass it to builder, so in production the connection string would be read from webconfig:
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MyWebconfigConnString")));
and I got stucked. Simply typing its name in builder’s method doesn’t work. Have I omitted something important between saving data in webconfig and using it in program.cs? Or is it my “connectionstring specific” mistake while trying to access it?