views:

77

answers:

3

Hi there,

I have been using Url.Content inside <% and %> in my views and all seems to be working fine... Then from withing my controller i have tried the following, but it always returns the wrong path

 XDocument xdoc = XDocument.Load(Url.Content("~/content/xml/faq.xml"));

and

 XDocument xdoc = XDocument.Load(VirtualPathUtility.ToAbsolute("~/content/xml/faq.xml"));

Basically the path shoudl be c:\Vs2008\Source\MyAppName.....

but its returning c:\MyAppName.....

So its invalid,

Any ideas why this is happning?? Is there a work around?

+2  A: 

That's correct. I'm not sure why it's adding the C:\ but MyApp...\ is the absolute path.

Have you tried Server.MapPath?

Chuck Conway
Thank you ... Yes Server.MapPath works
mark smith
+5  A: 

Those two methods are only meant to be used in the context of clients accessing content through your web server. To read a file internally, within the application you need to use Server.MapPath() or a similar method to get the physical path on your disk.

rmacfie
+1  A: 

I was having a similar problem linking to some .css and image files. I wrote a blog post on this at http://www.stickfiguresoftware.com/node/46 that may be helpful and even has some sample code that I got to work.

Not sure it's the perfect solution, but it worked out for me.

Mark Ewer