Monday, March 26, 2012

Web.config barring me from seeing images, stylesheets, and .js files

It seems that the trend is to have Web.config bar a user from just navigating to any random bit of content. The default MVC4 application runs great in Cassini, but on IIS at my Rackspace server, I cannot "see" the images in the Images folder, the stylesheet in the Content folder, or, I'm guessing, the .js files in the Scripts folder. I have not confirmed the third failing, but it would fit with the first two. How do I open up permissions to see this content? Beats me. I'll keep trying to find a solution. I have had little luck in Googling so far, but then MVC4 is also in Beta.

This is not a bug. This is a trend. Orchard CMS does the same thing. It only lets you "see" what is in the Media folder. It does not allow you to put images just anywhere. If you make an Images folder and stick a bunch of images in it, you're going to be frustrated. The Media folder has its own Web.config file which seems to allow for an exception to blocking everything. It looks like so:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <appSettings>
      <add key="webpages:Enabled" value="false" />
   </appSettings>
   <system.web>
      <httpHandlers>
         <add path="*" verb="*" type="System.Web.StaticFileHandler" />
      </httpHandlers>
   </system.web>
   <system.webServer>
      <handlers accessPolicy="Script,Read">
         <add name="StaticFile" path="*" verb="*" modules="StaticFileModule"
               preCondition="integratedMode" resourceType="File" requireAccess="Read" />
      </handlers>
   </system.webServer>
</configuration>

 
 

I tried to make a Web.config file like this in the Content folder of my MVC4 app. This "solution" did not work. Grrr. I'll keep trying and update this blog when I know more.

The Web.config stuff has always seemed like magic to me.

I suppose I could just hack around the problem by hosting anything with a .css, .gif, .jpg, .js, or .png extension at a subdomain.

6 comments:

  1. Hi, this is not related to MVC4 or even ASP.NET it is actually a behavior dictated by IIS. This behavior is controlled the setting known as "directory browsing". It is disabled by default for security reasons (more info at http://technet.microsoft.com/en-us/library/cc731109(v=WS.10).aspx).
    You are correct that Cassini didn't respect this setting.

    You can easily override this by creating a web.config in the folder in which you want to enable directory browsing. I wasn't able to post XML in this comment so I placed the content you need at https://gist.github.com/2222167. Hope that helps.
    Note: when you create this web.config it will enable directory browsing for the folder that it lives in as well as sub-folders (unless they have a web.config overriding the value).

    ReplyDelete
  2. Thank you for your help. This seems like a logical explanation, but this alone does not solve my issue. /Images/twitter.png is one of the images that comes by default in an MVC4 application. I cannot see the image in a browser by navigating to it at the URL line of a browser. When I rename the root level Web.config file to be _Web.config, then my app stops working, but I can navigate to /Images/twitter.png. When I change _Web.config back to Web.config, my app starts working again, but I can't see anything in the Images folder. I'm fairly confident that something in the default root level Web.config file for an MVC4 application is forbidding most content from being seen. Using the tricks you describe, I still cannot overcome the setting.

    ReplyDelete
  3. Have you tried creating a web.config with the content that I posted inside the images folder? I just tried this with an MVC 4 project, published to an IIS server and then browsed to http://mysite.com/images/ and it shows me the list of files in that directory. Are you saying that is not working for you? If so then perhaps something else is going on here.

    FYI You can also set this setting in the root web.cofig if you want (I wouldn't advise it, but that is an option).

    ReplyDelete
  4. Your fix did not work for me. I did try it. :) Perhaps my own configuration is just different from yours??? I had to ultimately turn to this solution: http://tom-jaeschke.blogspot.com/2012/03/i-found-way-around-mvc4-webconfig.html

    ReplyDelete
  5. Interesting so on my machine I must already have that patch. In any case sorry to hear you had these issues, but glad to hear you got them fixed!

    ReplyDelete