MVC 3 Won't Serve Content files from Areas sub

2019-03-10 04:32发布

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!

1条回答
女痞
2楼-- · 2019-03-10 05:05

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.

查看更多
登录 后发表回答