1

I suspect I may be wording this question incorrectly (it’s been a while since I’ve done anything in the Microsoft space) but here goes.

In essence, I’m trying to programmatically access an XML document with the ~/Content directory. I’ve tried to do this along the lines of…

XElement resourceConfigXML = XElement.Load(@"~/Content/resource_configuration.xml");

..but this obviously isn’t correct, as the string isn’t being converted into a path the operating system understands. As such, is there any means of deriving the physical path to this directory at run-time?

Incidentally, if this is a naïve idea destined to failure/a grotesque misunderstanding of the purpose of the Content directory, feel free to let me know – in my defence I’m only getting to grips with .NET (the MVC flavour thankfully).

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • 1
    This is a "grotesque" (your words) misunderstanding of the Content directory. This file can be called by any client who can guess the url. This is especially so if it contains information the user is not supposed to see. If it is config information put it in root along with web.config with the .config extension (IIS doesn't allow the client to access .config files). If it is more data in nature then put it in APP_DATA folder you can keep the .xml - the client cannot access this folder either. Use content for images, css and other non aspx files that the user should access. – Michael Gattuso Dec 04 '09 at 15:49
  • @Michael - Thanks for the feedback - I suspected as much, so your feedback is very much appreciate. App_Data it is. :-) – John Parker Dec 04 '09 at 16:00
  • Resolved nicely in a similar question: http://stackoverflow.com/questions/347528/using-scripts-in-a-master-page-with-asp-net-mvc – Mouffette Dec 04 '09 at 16:51
  • @Mouffette Thanks for the heads-up - the App Helper route looks like a neat solution to a more general issue, but it's good to know. – John Parker Dec 04 '09 at 17:04

3 Answers3

3

Try using Server.MapPath

XElement resourceConfigXML = XElement.Load(Server.MapPath(@"~/Content/resource_configuration.xml"));  
Li0liQ
  • 11,158
  • 35
  • 52
1

You want to change this to :

XElement.Load(Server.MapPath("~/Content/file.xml"))
Tim Ebenezer
  • 2,714
  • 17
  • 23
0

Another way if you want to refer to the domain instead of the machine path

XElement.Load(VirtualPathUtility.ToAbsolute("~/Content/file.xml"));
Cyril Gupta
  • 13,505
  • 11
  • 64
  • 87