Using Images in Web Applications and Visual Studio for ASP.NET MVC

I am developing ASP.NET MVC applications with MS Visual Studio 2008 SP1.

My default project structure:

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

The fact is that I can access all the content placed in the Content directory, provided that the file included in the project. On the other hand, if I have an image (IE image uploaded by the user) that is on the right physical directory (Project \ Content \ img), but not included in the project, I continue to get 404 when accessing them in the browser.

I believe my URL is correct:

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

And I have a file in Project \ Content \ img \ my_image.jpg.

What could be wrong? Am I forced to include all files in a project? I don’t think so, because it will mean that I cannot upload and save images by web users in this way.

Many thanks.

+3
source share
1 answer

If you host your project using IIS 7, you must add its content type to IIS 7 (Handler Mapping). But if you host your project using the Asp.net developer server, it is not required.

Using the following code in the web.config file

<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>
+4
source

Source: https://habr.com/ru/post/1707223/


All Articles