如何在视图中包含js文件。 ASP.NET MVC 4 [英] How to include js files in the view. ASP.NET MVC 4
本文介绍了如何在视图中包含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>
}
推荐答案
这是因为 .js
文件无法在〜/ Views /
文件夹中访问。你必须启用它。
It's because .js
files are not accessible in the ~/Views/
folder. You have to enable it.
启用 .js
文件的访问权限在Views文件夹中,您可以直接在处理程序
标记下将以下内容添加到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屋!
查看全文