In this article i will show you how you can access or read the email setting from Appsettings.json file in asp.net core 6 / MVC application using c#.net. So for this first we will create a new asp.net core web application and add open the Appsettings.json file. In Appsettings.json file we will first add the email server setting as shown below.
Other Articles: Retrieve Connection String From Web.config In ASP.net Using C#.Net (AppSettings,ConnectionStrings), Access Connection String In Asp.Net Core 6 Controller From appsettings.json C# , appsettings.json Connection String Value Access in Asp.net Core Controller, Access Connection String In Asp.Net Core 6 ClassLibrary From appsettings.json C# | appsettings.json Connection String Value Access in Asp.net Core ClassLibrary, Access Connection String In DbContext File From appsettings.json C# In Asp.Net Core 6.
"Smtp": { "Host": "smtp.xxxx.com", "EnableSsl":
true, "UserName":
"admin@xxxx.com", "FromEmail":
"admin@xxxxx.com", "Password":
"iNxxxx123", "DefaultCredentials": false, "Port": 123 } |
var
builder = new
ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false); IConfiguration config =
builder.Build(); string host = config.GetValue<string>("Smtp:Host"); string username = config.GetValue<string>("Smtp:UserName"); string fromemailid = config.GetValue<string>("Smtp:FromEmail"); string password = config.GetValue<string>("Smtp:Password"); string defaultcredentials = config.GetValue<string>("Smtp:DefaultCredentials"); string port = config.GetValue<string>("Smtp:Port"); string enablessl = config.GetValue<string>("Smtp:EnableSsl"); |
In
In above code i have used ConfigurationBuilder to read the appsettings.json file. The name space for ConfigurationBuilder is Microsoft.Extensions.Configuration. Now we will add the code to display it on controller view.
public
IActionResult Index() { var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false); IConfiguration config =
builder.Build(); string host = config.GetValue<string>("Smtp:Host"); string username = config.GetValue<string>("Smtp:UserName"); string fromemailid = config.GetValue<string>("Smtp:FromEmail"); string password = config.GetValue<string>("Smtp:Password"); string defaultcredentials = config.GetValue<string>("Smtp:DefaultCredentials"); string port = config.GetValue<string>("Smtp:Port"); string enablessl = config.GetValue<string>("Smtp:EnableSsl"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Host: " + host + "<br/>"); stringBuilder.Append("UserName: " + username + "<br/>"); stringBuilder.Append("FromEmail: " + fromemailid + "<br/>"); stringBuilder.Append("Password: " + password + "<br/>"); stringBuilder.Append("DefaultCredentials: " + defaultcredentials + "<br/>"); stringBuilder.Append("Port: " + port + "<br/>"); stringBuilder.Append("EnableSsl: " + enablessl + "<br/>"); ViewBag.Data = stringBuilder.ToString(); return View(); } |
Now lets write code to display the accessed configuration setting in controller view.
@{ ViewData["Title"] = "Home Page"; } <div> @ViewBag.Data </div> |
Now we have done run the code and check the output. But before checking the final output lets put the break point and check whether we are able to access the detail or not. so here we can see we are able to access the detail.
Download
0 comments:
Please let me know your view