Reputation: 3707
I have an MVC web application where I would like to read an XML file that will give me a list of image folders/files to display.
I'm running this locally on a Windows 10 machine using Visual Studio 2015. In my controller when I try to do the following code to get the XML file I immediately get an error "The Web server is configured to not list the contents of this directory."
public ActionResult Index()
{
XElement xelement = XElement.Load("~\\Gallery\\Gallery.xml");
IEnumerable<XElement> galleries = xelement.Elements();
return View();
}
I then did some research and found that I should put the following in my web.config file. When I do that and go to the View it will immediately just list out the folders in my directory.
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
I also followed some info where I went to IIS, selected Directory Browsing, and then "enable". That didn't seem to have any effect.
Any ideas on what I can do?
Upvotes: 0
Views: 636
Reputation: 3707
I'm posting an Answer to this question in case someone else comes across the same issue.
The problem ended up being the fact that I had a controller named "Gallery" as well as a folder in my site called "Gallery". My HTML ActionLink was creating a hyperlink like instead of . Once I changed the image folder to GalleryImages then everything worked properly.
Don't name your controllers the same as a folder name in your site.
Upvotes: 0
Reputation: 120450
"~\\Gallery\\Gallery.xml"
is not understood as the path you intended by XElement.Load
.
You can map ASP.NET application paths to file-system paths with HostingEnvironment.MapPath
, so:
XElement xelement = XElement.Load(HostingEnvironment.MapPath("~\\Gallery\\Gallery.xml"));
Upvotes: 2