简体   繁体   中英

Virtual Directory IIS and MVC

This is probably something that is a quick fix, but I cannot seem to locate an answer. I added a virtual directory to my site (MVC 4) and have it set to run as its own application. However, the image files are all trying to be accessed from the root of the site, not the virtual directory. I've changed the paths in the CSS files but that didn't seem to solve the problem. Do I really have to hard code the absolute paths to all my files vs. @Url.Content("~") ?

Referencing images from CSS

Make the paths in the stylesheet relative to the location of the stylesheet.

So, if your stylesheet is in Content\\Site.css and you want to reference an image in Images , use the path '../Images/<filename>' in Site.css . That way, the path will be correct no matter what virtual directory your app is served from.

In other words, to use myimage.png as a background image, you would put something like this in Site.css :

#mydiv { background-image: url('../Images/myimage.png'); }

Referencing images from Razor view

When referencing a static image from a razor view, use the @Url.Content("~/pathtofile") method, or, better yet, use T4MVC (see section 2.4 on that page).

Hope this helps.

Hardcoding is a bad idea. Use @Url.Content() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM