0

I have a view inside my Views folder in my MVC 3 application which loads CSS, Images and JS files from a folder at root level of the application. On running the View get the following output, I tried using

  • <link href="@Url.Content("~/RootFolder/styles.css")" rel="stylesheet" type="text/css" /> for CSS and below code which did not work

    <script src="@Url.Content("~/RootFolder/jquery-1.8.3.min.js")" type="text/javascript"></script>

    • I added in my views Web.config file

Cannot load JS,CSS and images from root folder

I cannot load CSS/Images/JS files from the root folder, But if I place the cshtml file in the root folder where CSS/Images/JS files are present the application works as expected displaying everything. Can you please help me resolve this issue

Thanks for your help :-)

UPDATE: My HTML code looks as shown in the below image

HTML Code looks like this

My folder hierarchy in the application looks as

Folder Hierarchy in my application

NaN
  • 1,012
  • 3
  • 13
  • 25
  • It might not be related to your problem, but you probably want to load javascript using bundles, instead of urls embedded into the html. The same for CSS (http://www.asp.net/mvc/overview/performance/bundling-and-minification) – Pablo Romeo May 10 '15 at 00:55
  • 1
    Can you look into the page source-code (rendered on the browser) and tell us what are the links looking like over there? – Marcelo Myara May 10 '15 at 01:09
  • Thanks for your quick comments Pablo Romeo and Marcelo Myara, I have added a new picture to this question which shows you the HTML code generated for the application. Content/SportTemplate is where my CSS/JS/images files present and my view is present in Views/Home folder. Thanks – NaN May 10 '15 at 18:07
  • 1
    Rather than some pictures, include the HTML itself. Also, `~/` is the virtual path to the site root, so where are your CSS files, etc. located in relation to root? – Tieson T. May 10 '15 at 18:10
  • layout view code works as it is without any issues but issue raised when Index view is trying to use layout view – NaN May 10 '15 at 18:22

1 Answers1

1

If you are using MVC4 and above, you wouldnt need to use @url.content, give the path directly.

Also, there is a difference between application root and website root. To figure that out, click on View Page source while application is running on the localhost and check out what the url is converted into. You gave out your html in pastebin, but that woldnt help. Give the page source after running on the browser to see the difference in the URL.

I recommend you to check out this and other answers on this question, they will clear up a lot of stuff for you. I'm pretty sure its the root difference, if you need more help, you can share your project too and I can check and share your code after running on browser.

Community
  • 1
  • 1
Polynomial Proton
  • 5,020
  • 20
  • 37
  • Yep that did the trick, removed @url.content definitions from my code and everything works normal now. Thanks for the help ! – NaN May 10 '15 at 19:28