gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:50:16 25 4
gpt4 key购买 nike

我正在使用 MS Visual Studio 2008 SP1 开发 ASP.NET MVC 应用程序。

我的项目结构是默认的:

Project |-Content |-css |-img |-Models |-Views |-Controllers

问题是,如果文件包含在项目中,我就可以访问Content 目录下的所有内容。另一方面,如果我碰巧有一张图片(即用户上传的图片)位于正确的物理目录 (Project\Content\img) 但不包含在项目中,我会一直收到 404 当我使用浏览器访问它们时出错。

我认为我的网址是正确的:

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

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

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

非常感谢。

最佳答案

如果您使用 IIS 7 托管项目,则必须在 IIS 7(处理程序映射)中添加内容类型。但如果您使用 Asp.net Developer Server 托管您的项目,则不需要。

在 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>

关于c# - 在 Web 应用程序和 Visual Studio for ASP.NET MVC 上使用图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/796007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com