2

I'm new here. I've started working my own forum system recently to use as a portfolio. Decided to let the admin of the website set his own name and description for the forum. So first thought was use .ini file to put the string there but C# does not support inis but it does XML files. So I have created a simple XML file for use.

But my question is where do I read all the values from that XML file to send them to _Layout every time.

Because if I send it through the View() from each function in the controller that is not really good.

Can somebody point out where in the website to read the values and send them to the _Layout

Kyojin
  • 157
  • 2
  • 12

3 Answers3

0

My answer will not be for XML, but for Resource files. You can use them to annotate your models or even use them in the views directly

Here is a article about resource files http://www.devcurry.com/2013/02/aspnet-mvc-using-resource-files-to.html

  • This looks like a solution, tho how can I let the user modify those? Is that even possible. Because it looks like I can only modify it in the source. – Kyojin Jun 15 '15 at 20:27
  • It is in fact an xml file. You can use notepad or programs specially made for resx files http://www.webpronews.com/net-resource-editor-app-for-resx-files-2006-10 And if you want to do it proper you should use the resx files – Stoyan Uzunov Jun 15 '15 at 21:11
0

You can use xml, of course, but you can also use JSON, it's smaller in size and it's the general community standard nowadays.

Either way you can read a file in a .NET web app simply using this:

string viewPath = HttpContext.Current.Server.MapPath("~/Content/Templates/your-xml_file.xml");
                var template = File.ReadAllText(viewPath);

and then use wherever library you want to read the xml file (or you can deserialize it to an object and use it to make things easy for you instead of navigating in the nodes fo the xml file.

If you use JSON data you can use the Newtonsoft.Json.JsonConvert class to read and deserialize.

Coastpear
  • 374
  • 2
  • 15
  • That's what I wanted. But I don't know how to send that data from the json/xml to the _Layout. But I think I'm coming up with solution. – Kyojin Jun 15 '15 at 20:28
0

Sourced: from this link

The web.config (or app.config) is a great place to store custom strings:

in web.config:

<appSettings>
   <add key="message" value="Hello, World!" />
</appSettings> 

in cs:

string str = ConfigurationSettings.AppSettings["message"].toString();
Community
  • 1
  • 1
Brad C
  • 2,868
  • 22
  • 33