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

3
  • Why would you do this? The scripts go in the script folder for a reason. Organization. Commented Dec 13, 2014 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. Commented Dec 13, 2014 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". Commented Dec 16, 2014 at 7:10

3 Answers 3

3
+50

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.