0

Is it possible in ASP.NET (not MVC) in IIS7.5 on Windows Server 2008R2 to read an XML file from a subfolder beneath the web app using a relative URL?

Something like:

     Dim rdr As XmlTextReader
     rdr  = New XmlTextReader( "xml\foo.xml")    // have also tried "~/xml/foo.xml"

Given this folder hierarchy:

     webapp
       styles
       images
       xml

I haven't been able to get it to work. If it's possible, is my relative url not formed correctly?

P.S. I am trying to use HTTP protocol so I can see the request being made in the Network monitor in the browser Developer Tools, and also to avoid file systems permissions issues. I had it working using the physical path in Windows Server 2003 but that code doesn't work in 2008R2 and I haven't been able to figure out which virtual user or machine identity needs permissions. I've given permissions to IUSR and to ASP.NET v4.0 Application Pool Identity.

Tim
  • 8,669
  • 31
  • 105
  • 183
  • You should no longer use `New XmlTextReader` as of .NET 2.0. Use `XmlReader.Create` instead. – John Saunders Apr 10 '13 at 18:20
  • The old constructor accepted a URL. I am hoping to use HTTP protocol because reading from the file system is not working in 2008R2 as it used to work in 2003. What built-in account does the XMLReader use? – Tim Apr 10 '13 at 19:01
  • Why would it not use the same account as you're running under? A URL is still permitted: [XmlReader.Create(String)](http://msdn.microsoft.com/en-us/library/w8k674bf.aspx) – John Saunders Apr 10 '13 at 19:04
  • Is a *relative* URL permitted is my original question. – Tim Apr 10 '13 at 19:23
  • Relative to _what_? Of course not. Why would `XmlReader` know what your "current" URL is to figure out what the absolute URL should be? – John Saunders Apr 10 '13 at 20:20
  • I wanted to know if there was something *analogous* to MapPath that would map the path relative to the web app home folder so I could use HTTP. – Tim Apr 11 '13 at 00:52
  • If you're on a page or user control, you can use [ResolveUrl or ResolveClientUrl](http://stackoverflow.com/questions/518199/control-resolveurl-versus-control-resolveclienturl-versus-virtualpathutility-toa). – John Saunders Apr 11 '13 at 01:19

2 Answers2

0

Have you attempted to use tilde(~) in your URL?

The tilde represents the the root directory of the application in ASP.NET.

e.g.

XmlTextReader reader = new XmlTextReader("~/xml/foo.xml");
Timothy Randall
  • 17,634
  • 1
  • 15
  • 27
  • Thanks for the suggestion, but I have tried it and it didn't work. I will edit my question to show this. – Tim Apr 10 '13 at 18:15
0

As with any file system path in ASP.NET, you have to use Server.MapPath:

Dim rdr as XmlReader = XmlReader.Create(Server.MapPath("~/xml/foo.xml"))

If you're trying to use HTTP and use a relative path from your web site, then I suggest you stop trying. If you know the URL to the root of your site, then just try http://mysite/xml/foo.xml.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • MapPath does not create a relative path that the HTTP protocol can handle but resolves to `file:///server/wwwroot/webapp/xml/foo.xml` – Tim Apr 10 '13 at 18:48
  • Note that you said nothing about needing http. Why not just read the file from disk? – John Saunders Apr 10 '13 at 18:49
  • I did mention the HTTP in my P.S. I could read from disk on Windows Server 2003 but we are porting the app to 2008R2 IIS7.5 and I am not able to read from disk because of some permissions issue which is driving me bananas. I've been trying to solve it for three days. What built-in account would the XMLReader be using when reading from disk? I need to give that account, whatever it is, ACL permissions to the XML folder. It's not the ASP.NET v4.0 Application Pool Identity or IUSR because these accounts both have ACL permissions already. Everything is working but this XML stuff. – Tim Apr 10 '13 at 19:12
  • There's nothing magic about `XmlReader` that would cause it to use a different account. It's going to use the same account which would have been used by `New StreamReader(url)`. – John Saunders Apr 10 '13 at 19:18
  • What account might that be? `...would have been used` : These app pool identities did not exist on Windows Server 2003. – Tim Apr 10 '13 at 19:20