如何在视图中包含 js 文件.ASP.NET MVC 4 [英] How to include js files in the view. ASP.NET MVC 4

查看:36
本文介绍了如何在视图中包含 js 文件.ASP.NET MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在视图中调用它时,我想知道为什么我的 js 文件可以工作:

I wonder why my js file work when I call it in the view:

@section Scripts {  
<script>   

    function myFunction() {
        alert("Hello1");
    }       

</script>
}

但是当我调用它时不起作用:

but does not work when I call it:

@section Scripts {

   <script type="text/javascript" src="~/Views/Home/script.js"></script>
   <script>   
         myFunction();
    </script>
}

推荐答案

这是因为 ~/Views/ 文件夹中的 .js 文件无法访问.你必须 启用它.

It's because .js files are not accessible in the ~/Views/ folder. You have to enable it.

要允许访问 Views 文件夹中的 .js 文件,您可以直接在 handlers 标记下将以下内容添加到 Views 文件夹的 web.config 中:

To enable access to .js files in the Views folder, you can add the following to your Views' folder's web.config directly under the handlers tag:

<add name="JavaScriptHandler"
         path="*.js"
         verb="*"
         preCondition="integratedMode"
         type="System.Web.StaticFileHandler" />

或者将您的脚本放入 ~/Scripts/ 文件夹并像这样引用它:

Alternatively put your script into the ~/Scripts/ folder and reference it like such:

@Scripts.Render("~/Scripts/script.js")

这篇关于如何在视图中包含 js 文件.ASP.NET MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆