Adding Javascript to another folder

How can I link JavaScript to another folder (except the Script folder). If I do this:

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

The browser tells me that the javascript file was not found. Can this be done or should all my scripts be in the Scripts folder?

Basically, I want my Javascript to be included in my View subfolders.

+6
source share
3 answers

You should change web.config in the Views folder according to this answer: In ASP.NET MVC, how can I load a 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> 
+3
source

Since you specifically want to include only javascript files, make the following change in the ~/Views/web.config : add 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 save all current file locks other than javascript.

+1
source

I donโ€™t know if I understood your question correctly, but if you want to use the script file from any folder in the view or preferably in its layout, you can add the following tag to the <head> section of you View or _Layout.cshtml :

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

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

0
source

Source: https://habr.com/ru/post/979539/


All Articles