views:

80

answers:

4

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).

+3  A: 

Try using Server.MapPath

XElement resourceConfigXML = XElement.Load(Server.MapPath(@"~/Content/resource_configuration.xml"));
Li0liQ
Many thanks - time for some proper playing around now. :-)
middaparka
+1  A: 

You want to change this to :

XElement.Load(Server.MapPath("~/Content/file.xml"))
Tim
A: 

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

XElement.Load(VirtualPathUtility.ToAbsolute("~/Content/file.xml"));
Cyril Gupta
+1  A: 

Resolved nicely in a similar question: http://stackoverflow.com/questions/347528/using-scripts-in-a-master-page-with-asp-net-mvc

Mouffette
@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.
middaparka