简体   繁体   中英

ASP.NET Core 2.0 MVC 6. How to manage javascript file for each view?

ASP.NET Core MVC 6 How to manage javascript file for each view? I have view specific javascript file and i dont want to place js file in script folder coz three are more than 100 views in my project. Is there any idea to place js file with views in view folder or any idea ?

Typically Visual Studio does not give you the option to create .js files in a View folder. The easiest way to do this is to create your .js files in your wwwroot and drag them over into your view folder.

You'll then need to update your bundleconfig.json to pull the contents of your .js files from your Views folder to build your .js files in your wwwroot.

The reason for this is because MVC expects .js files to be included in your wwwroot content, and deploys that folder all together and configures your web server to serve the content from there. MVC does not expect your .js to be in your Views folder and therefore your web server won't serve .js files from there.

So if you configure things how I said, your .js will be in your Views folder for writing code only, and then your bundling will move your code for you automatically.

Make sure, additionally, that you go into the Task Runner Explorer window to configure your bundle tasks. Please see this article for more information on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM