代码之家  ›  专栏  ›  技术社区  ›  Pablo Santa Cruz

在Web应用程序和Visual Studio for ASP.NET MVC上使用图像

  •  3
  • Pablo Santa Cruz  · 技术社区  · 15 年前

    我的项目结构是默认结构:

    项目|-内容|-css |-img |-模型|-视图 |-控制器

    问题是,我能够访问放置在下面的所有内容 提供该文件的目录 使用浏览器访问它们时出错。

    我认为我的URL是正确的:

    http://localhost:1260/Content/img/my_image.jpg

    在Project\Content\img\my_image.jpg下有一个文件。

    有什么不对劲?我是否必须包含项目中的所有文件?我不这么认为,因为这意味着我不能让网络用户以这种方式上传和保存图像。

    谢谢。

    1 回复  |  直到 8 年前
        1
  •  4
  •   Chris B. Behrens    13 年前

    如果您使用IIS 7托管项目,则必须在IIS 7(处理程序映射)中添加内容类型。但是,如果您使用Asp.net开发人员服务器托管项目,则不需要这样做。

    在web.config文件中使用以下代码

    <configuration>
       <system.webServer>
         <handlers>
          <add name="css mapping" path="*.css" verb="*" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode" />
          <add name="js mapping" path="*.js" verb="*" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode" />
          <add name="gif mapping" path="*.gif" verb="*" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode" />
          <add name="jpg mapping" path="*.jpg" verb="*" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode" />
          <add name="png mapping" path="*.png" verb="*" type="System.Web.StaticFileHandler" resourceType="Unspecified" preCondition="integratedMode" />      
         </handlers>
       </system.webServer>
    </configuration>