Cannot display image (404)

I am upgrading an existing MVC5 project to MVC6. However, when I try to get the image to display in the MVC6 project, it should be 404s. Image Path:

/Content/images/image.png 

In my MVC5 project, the following actions are performed:

  <img src="@Url.Content("~/Content/images/image.png")" /> 

I tried the following:

  <img src="@Url.Content("~/Content/images/image.png")" /> <img src="@Url.Content("/Content/images/image.png")" /> <img src="@Url.Content("Content/images/image.png")" /> <img src="@Url.Content("~Content/images/image.png")" /> <img src="~/Content/images/image.png" /> <img src="/Content/images/image.png" /> <img src="Content/images/image" /> <img src="~Content/images/image.png" /> <img src="C:\absolute\path\Content\images\image.png" /> 

Is there a way to reference an image modified using MVC6?

+4
source share
2 answers

You need to put the image in the wwwroot folder. Only static files in this folder are served. You also need to make sure that you add static file support to the Startup.cs file.

 public void Configure(IApplicationBuilder application) { // Add static files to the request pipeline eg hello.html or world.css. application.UseStaticFiles(); // Add MVC to the request pipeline. application.UseMvc(); } 
+9
source

Although it seems to me that it is better to move the files to the wwwroot folder, as in the accepted answer, to follow the standard conventions, you can also change the path to the static files through the configuration.

In the project.json file, change the configuration attribute to specify the path to the static file.

 "webroot": "wwwroot" 

Just an alternative approach.

+5
source

All Articles