6

How can I have JavaScript bundling working from another folder (aside from the Script folder). If I do this:

bundles.Add(new ScriptBundle("~/bundles/search").Include("~/Views/Search/*.js"));

The browser tells me the javascript file can't be found. Is it possible to do this or do all my sripts have to be in the Scripts folder?

Basically I want my Javascript included in my View subfolders

jww
  • 97,681
  • 90
  • 411
  • 885
PoeHaH
  • 1,936
  • 3
  • 28
  • 52
  • Why would you do this? The scripts go in the script folder for a reason. Organization. – Trucktech Dec 13 '14 at 17:37
  • 2
    The concept of organization is different for everyone. Some people like to organize by color, others like to organize by alphabet,... I'd like my javascript to be in the same folder as the View they'll be used on. – PoeHaH Dec 13 '14 at 18:16
  • I would recommend using an "assets" folder in your root folder. I normally use "assets/css", "assets/scss", "assets/js" and "assets/images". – janhartmann Dec 16 '14 at 07:10

3 Answers3

3

You need to change web.config in Views folder according this answer: In ASP.NET MVC, how can I load script from my view folder?

Good example from Ashley Lee:

<system.webServer>
  <handlers>
    <add name="JavascriptViewHandler" path="*.js" verb="*"
        preCondition="integratedMode" type="System.Web.StaticFileHandler" />
    <remove name="BlockViewHandler"/>
    <add name="BlockViewHandler" path="*" verb="*"
      preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  </handlers>
</system.webServer>
Community
  • 1
  • 1
offi
  • 380
  • 8
  • 13
  • You should include the relevant parts of the answer here with the question (rather than cite another source). There's a Stack Overflow close reason just for that transgression. That's just how Stack Overflow works. – jww Dec 21 '14 at 08:43
  • Thank you very much! Saved me a lot of time. – user3413723 Dec 01 '17 at 14:23
1

Since you specifically want to only include javascript files, make the following change to your ~/Views/web.config file, by adding the "JavascriptViewHandler" section.

<system.webServer>
  <handlers>
    <add name="JavascriptViewHandler" path="*.js" verb="*"
        preCondition="integratedMode" type="System.Web.StaticFileHandler" />
    <remove name="BlockViewHandler"/>
    <add name="BlockViewHandler" path="*" verb="*"
      preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  </handlers>
</system.webServer>

This will preserve all of the current blocking for non-javascript files.

Ashley Lee
  • 3,810
  • 1
  • 18
  • 26
0

I don't know if i understood your question properly, but if you want to use a script file from any folder in a View or preferably in it's Layout, you can add the following tag in <head> section of you View or _Layout.cshtml:

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

You can mention the complete path to your script file instead of ~/Scripts/jquery-ui.min.js

Saket Kumar
  • 4,363
  • 4
  • 32
  • 55