A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
so on startup or program.cs how to load file nd on razor page loadcolor.cs and loadcolor.cs.html how to load value of GOLD
You can use the following methods:
Note: The following sample is an Asp.net 7 Razor page application and the XML file is in the wwwroot/files folder.
1.Using IConfiguration
You can use the XML configuration provider to load the configuration from the xml file, then access the value via the IConfiguration.
Code like this: In Program.cs file:
builder.Configuration.AddXmlFile(Path.Combine(builder.Environment.WebRootPath, "files", "DeafaultValues.xml"), optional: true,
reloadOnChange: true);
var gold = builder.Configuration["configuration:GOLD"];
In the Razor page Index.cshtml.cs file:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IConfiguration _configuration;
public IndexModel(ILogger<IndexModel> logger,
IWebHostEnvironment webHostEnvironment,
IConfiguration configuration)
{
_logger = logger;
_webHostEnvironment=webHostEnvironment;
_configuration=configuration;
}
[BindProperty]
public string Gold { get; set; }
[BindProperty]
public string Gold2 { get; set; }
public void OnGet()
{
Gold = _configuration["configuration:GOLD"]; //get value via IConfigration.
}
}
2.Using LINQ To XML.
In the Program.cs file, we can use the following code:
//required using System.Xml.XPath;
XDocument po = XDocument.Load(Path.Combine(builder.Environment.WebRootPath, "files", "DeafaultValues.xml"));
// LINQ to XML query
XElement gold2 = po.Root.XPathSelectElement("configuration//GOLD");
var goldvalue = gold2.Value;
Then in the Index.cshtml.cs page, we can use the code as below:
//required using System.Xml.XPath;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IConfiguration _configuration;
public IndexModel(ILogger<IndexModel> logger,
IWebHostEnvironment webHostEnvironment,
IConfiguration configuration)
{
_logger = logger;
_webHostEnvironment=webHostEnvironment;
_configuration=configuration;
}
[BindProperty]
public string Gold { get; set; }
[BindProperty]
public string Gold2 { get; set; }
public void OnGet()
{
Gold = _configuration["configuration:GOLD"]; //get value via Iconfiguration.
XDocument po = XDocument.Load(Path.Combine(_webHostEnvironment.WebRootPath, "files", "DeafaultValues.xml"));
// LINQ to XML query
XElement gold = po.Root.XPathSelectElement("configuration//GOLD");
Gold2 = gold.Value;
}
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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,
Dillion