Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 Won't Serve Content files from Areas subfolder

I have an MVC3 application with a couple of areas and a portable area as well (using MVCContrib)

Normally, I keep all my content files under ~/Content and my scripts under ~/Scripts.

However, I am building a fairly complex webclient to another service on my site and I want to organize those javascript and image files (LOTS of image files and resources) under the Area's folder structure, which looks something like this, under ~/Areas/WebClient

  • Content
    • css
    • fonts
    • images
    • js
  • Controllers
  • Models
  • Views

I have a resource aggregator controller (one of my portable areas) that is able to reach into the CSS/JS folders just fine to provide that content. However, the CSS files reference the images/fonts folders directly and all of those links show up broken. I have double and triple checked the paths and made sure everything was right but I still get 404 errors.

As far as I know MVC3 is supposed to ignore routing so long as there's a static file there. Also, as far as I know, only the App_* folders enjoy special protection. What am I missing? I'd rather not mix in my images and resources with my main application if I can at all avoid it.

As an example: http://localhost/Areas/WebClient/Content/images/knownimage.png will not work, but should, as it exists!

like image 514
Vassi Avatar asked Sep 21 '11 07:09

Vassi


People also ask

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.

How to implement bundling and minification in MVC?

To enable bundling and minification, set the debug value to "false". You can override the Web. config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.

What is the use of BundleConfig in MVC?

To improve the performance of the application, ASP.NET MVC provides inbuilt feature to bundle multiple files into a single, file which in turn improves the page load performance because of fewer HTTP requests.

How to create bundling in MVC?

This RegisterBundles() method contains all the bundles created in the application. Notice that this method has a parameter bundle, it is of type BundleCollection class. All the bundles created are added to this bundle parameter at the start of the application. For scripts(.


1 Answers

So after some sleep and, more importantly, stepping away from the problem I remembered that MVC does in fact offer you protection from people downloading views directly, which led me to remember the Web.config file required in the Areas folder. Sure enough, there's an httphandler that basically sends all requests to the FileNotFound handler.

All I had to do was drop a web.config file in the content folder I wanted to expose with the following:

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="BlockViewHandler" />     
    </handlers>
  </system.webServer>
</configuration>

Problem solved.

like image 53
Vassi Avatar answered Nov 11 '22 08:11

Vassi