Search code examples
javascriptasp.net-mvcasp.net-mvc-4publish

why .js files and css file if they are in view folder not getting served in asp.net MVC4


If .js files and css file are in view folder then they are not getting served. ANy idea what fix is required to make them both work if residing in same folder.

Path : Views\Shared\Javascripts
       Views\Shared\abc.cshtml

Solution

  • If you look in the ~/Views (or ~/Areas/<area name>/Views, if you are using areas) folder, you'll notice a web.config that prevents any resources from being served from that location. While you could edit that web.config to allow your static assets to be served, I would strongly caution you against this.

    Instead, place the assets in another folder. By default MVC projects have a ~/Content and ~/Scripts folder (CSS/images and JS, respectively). I'd encourage you to just stick to those folders.

    Edited to reflect recent edit

    Say you have abc.js. Move it to ~/Scripts/abc.js. You can include that script in abc.cshtml like this:

     <script type="text/javascript" src="@Url.Content("~/Scripts/abc.js")"></script>