Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP .Net MVC - Images not being shown in published build

I am developing an ASP .Net MVC application and on my dev machine, the application runs as expected and, more importantly, the images mentioned in the CSS file are displaying correctly too.

However, when I publish this application to a testing server, the web app runs fine, but the images are not shown.

If I modify the URL in IE when testing the output from the test server, the image is returned, meaning that the file is there but it just won't appear within the view page when using the site normally.

I have tried alternative servers too, but the result is the same.

To confirm, here's a line from the CSS page referencing the image...

background-image: url('/Content/Images/Logo/myLogo.jpg');

Any suggestions?

Cheers

Brett

like image 913
Brett Rigby Avatar asked May 30 '09 14:05

Brett Rigby


People also ask

Where do images go in MVC?

Images are stored in /Content/Images.

How do I display an image in razor view?

Step 1 - Go to SQL Server Management System and execute the following script. Step 2 - Go to Visual studio and add a new project. Select “Asp.Net MVC 4 Web Application” and give the name for this ,In my case it is “MVCDemoProject. ” -> Select a Project template as Empty and View engine is “Razor”, Then Click OK.


1 Answers

The URLs are not correct, likely due to the fact that you are publishing in a subfolder and so they are no longer at the root of the server. I usually use Url.Content( "~/Content/Images/..." ) to build the url instead of hard-coding it. That way it will take into account the routes when building the path.

Example:

 <img src='<%= Url.Content( "~/Content/Images/banner.jpg" ) %>' alt="Banner" />
like image 66
tvanfosson Avatar answered Oct 13 '22 19:10

tvanfosson