Understanding How to Retrieve Collection Objects from appsettings.json in .NET Core

preview_player
Показать описание
---

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---

The Challenge

[[See Video to Reveal this Text or Code Snippet]]

[[See Video to Reveal this Text or Code Snippet]]

And in your controller, you try to access the LineOfBusinesses property from the options parameter. However, upon running your application, you discover that LineOfBusinesses is null. The strings and other properties are populated correctly, leaving you perplexed.

The Root of the Issue

The problem here stems from the naming convention used in your CroutonStripePaymentGatewayOptions class. The property name LineOfBusinesses does not match the name used in your configuration, which is LinesOfBusiness. This discrepancy is what causes the binding to fail for the collection, resulting in null values.

The Solution

To resolve this issue, the property within your CroutonStripePaymentGatewayOptions class needs to be renamed to match the configuration exactly. Here's how you can do that:

[[See Video to Reveal this Text or Code Snippet]]

Steps to Implement the Solution

Rename the Property: Change LineOfBusinesses to LinesOfBusiness in your CroutonStripePaymentGatewayOptions class. This ensures the name matches the JSON configuration.

Rebuild Your Application: Save your changes and rebuild your application to ensure that the new naming conventions take effect.

Test the Configuration: Run your application again and verify that LineOfBusinesses is populated correctly this time.

Confirming the Fix

After implementing these changes, you should have a functional controller constructor like this:

[[See Video to Reveal this Text or Code Snippet]]

Conclusion

Рекомендации по теме
join shbcf.ru