Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appsettings.json get string with two programs running

I'm using run and debugging from VS Code, but when i run both projects API and MVC, give me error.

An exception of type 'System.ArgumentNullException' occurred in Microsoft.EntityFrameworkCore.SqlServer.dll but was not handled in user code: 'Value cannot be null.'

My code:

options.UseSqlServer(Configuration["Data:UCASAppDatabase:ConnectionString"]));

This happen after i run both projects using debug, but if i create appsettings outside API work, but what i want is work with appsettings inside API.

How i get string from appsettings.json, without creating new appsettings.json?

Project Folders


1 Answers

It's generally best to keep them separate, but if you really want to use just one, you should be able to add the appsettings.json file as a linked item in the project that doesn't contain the physical file. Use add => existing item, select "Add as Link" from the Add drop-down and select the file(s) you want to add. Make sure their build action is Content + "Copy if Newer" and you should be good to go. You might consider making a solution folder and putting shared resources there so it's more apparent that you have shared resources.

Edit: You should be able to manually add if VS Code doesn't have a way through the UI. Editing the .csproj file and add the content files you need:

<ItemGroup> 
    <Content Include="..\Root\appsettings.json" Link="appsettings.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>
like image 62
Mike-314 Avatar answered May 21 '26 21:05

Mike-314