简体   繁体   English

在asp.net mvc中添加JavaScript文件

[英]Add JavaScript files in asp.net mvc

I am new in asp.net mvc and I am trying to include .js files in my project but I could not access it on my browser. 我是asp.net mvc新手,我试图在我的项目中包含.js文件,但我无法在浏览器上访问它。 Like it. 喜欢它。

@Scripts.Render("~/bundles/responds.js")
@Scripts.Render("~/bundles/jquery-1.11.3.min.js")
@Scripts.Render("~/bundles/jssor.slider-22.0.15.mini.js")
<script type="text/javascript">
</script>

Anyone can help me, how can I add these files in mvc project? 任何人都可以帮助我,我怎样才能在mvc项目中添加这些文件? These files exists in Scripts folder. 这些文件存在于Scripts文件夹中。

You can manually add a .js file to a view with the following example code 您可以使用以下示例代码手动将.js文件添加到视图中

@model YourNameSpace.ViewModels.YourViewModel
@Scripts.Render("~/Scripts/yourScript.js")

If you want the files to be bundled & you can add them in App_Start/BundleConfig.cs 如果您希望捆绑文件,可以在App_Start/BundleConfig.cs添加它们

Example: 例:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle(("~/bundles/customBundle").Include(
     "~/Scripts/yourScript.js", 
     "~/Scripts/anotherScript.js");
}

You can render your script files for the whole application in the ~/Views/Shared/_Layout.cshtml file or any master page by adding the following code 您可以通过添加以下代码在~/Views/Shared/_Layout.cshtml文件或任何master page呈现整个应用程序的脚本文件

@Scripts.Render("~/bundles/customBundle")

You can inspect the .js file by browsing to the root of your application & add the path of the .js file or bundle (for this example) 您可以通过浏览到应用程序的根目录来检查.js文件,并添加.js文件或bundle的路径(对于此示例)

http://localhost:9654/bundles/customBundle
http://localhost:9654/Scripts/yourScript.js

I have solved my problem by just drag and drop script files from Scripts folder to at desired place in Index.cshtml and Visual studio auto generate below code. 我已经解决了我的问题,只需将脚本文件从Scripts文件夹拖放到Index.cshtml中的所需位置, Visual studio自动生成以下代码。

<script src="~/Scripts/jquery-1.11.3.min.js"></script>
<script src="~/Scripts/jssor.slider-22.1.5.mini.js"></script>

Which includes the script files in asp.net MVC . 其中包括asp.net MVC的脚本文件。

You can use C# code to calculate & generate the right file address as follows: 您可以使用C#代码计算和生成正确的文件地址,如下所示:

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

This pattern can use in *.cshtml files. 此模式可用于* .cshtml文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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