MVC 3 Won't Serve Content files from Areas subfolder

自古美人都是妖i 提交于 2019-12-03 06:18:26

问题


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:


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.



来源:https://stackoverflow.com/questions/7495780/mvc-3-wont-serve-content-files-from-areas-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!