not able to read configurations from appsettings.json in Blazor pre-rendering app

Keerthi Mettu 1 Reputation point
2021-06-21T23:29:19.05+00:00

please see this link for a detailed description on the problem

Please let me know if there is anything that can object from retrieving this appsettings.json file data in Blazor Web assembly pre-rendering.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,492 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Chao Deng-MSFT 796 Reputation points
    2021-06-22T03:04:15.277+00:00

    Hi @Keerthi Mettu ,

    I wrote a demo, you can refer to it and you can read the value.
    I first added the test attribute in appsetings.json.

     "MySettings": {  
        "DbConnection": "abc",  
        "Email": "abc@domain.com",  
        "SMTPPort": "5605"  
      }  
    

    Now, in our ConfigController, we will add a private field of type “IConfiguration” which is found in Microsoft.Extensions.

    public class ConfigController : Controller  
        {  
            private IConfiguration configuration;  
            public ConfigController(IConfiguration iConfig)  
            {  
                configuration = iConfig;  
            }  
            public IActionResult Index()  
            {  
      
                string dbConn = configuration.GetSection("MySettings").GetSection("DbConnection").Value;  
                
                  
                  
                return View();  
            }  
    

    Rseult:
    vex5q.png
    Choose the corresponding field for any attribute you need, or you can define a model to get it all at once.


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    ChaoDeng


  2. Keerthi Mettu 1 Reputation point
    2021-06-22T16:19:20.35+00:00

    Hi @Chao Deng-MSFT , thank you for your response. In my Blazor project, I don't have a controller rather a database connection management service class. But if you see in the following code, none of those variables (c, con, connectionstr) are providing a value instead of null. Can you please let me know if there is anything I'm missing.

     public class DataAccessors  
        {  
            private readonly IConfiguration Configuration;  
      
            public DataAccessors(IConfiguration configuration)  
            {  
                Configuration = configuration;  
            }  
            public DataAccessors()  
            {  
            }  
            public MySqlConnection CSONLINE_MySqlConnection()  
            {         
               string c =  Configuration.GetConnectionString("localDevDb");  
               // string con =  ConfigurationManager.ConnectionStrings["localDevDb"].ConnectionString;   
               var connectionstr = ConfigurationManager.AppSettings["ConnectionStrings.localDevDb"];  
                string conStr = "server=localhost;database=db.........port=3306";  
                using (MySqlConnection mysqlConnection = new MySqlConnection(conStr))  
                {  
                    return mysqlConnection;  
                }  
            }  
    }