ASP.NET MVC视图渲染不正确的脚本标签

编程入门 行业动态 更新时间:2024-10-26 00:28:54
本文介绍了ASP.NET MVC视图渲染不正确的脚本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个非常简单的MVC项目,并在我的_Layout.cshtml我有一些JS包括像这样:

I have a very simple MVC project, and in my _Layout.cshtml I have some js includes like so:

<script src="~/scripts/jquery-2.0.3.min.js"></script> <script src="~/scripts/easing.js"></script> <script src="~/scripts/bootstrap.js"></script>

然而,当它呈现,它呈现像在页面上(注意在第一个波浪号):

However, when it renders, it renders on the page like (note the tilde on the first one):

<script src="~/scripts/jquery-2.0.3.min.js"></script> <script src="/scripts/easing.js"></script> <script src="/scripts/bootstrap.js"></script>

我看来不能让它正确地呈现,而且它并不重要的script标签是第一个,这是它增加了一个/不断的波浪。我已经使出了包括jQuery脚本两次,第一个将有一个波浪线,但第二个将获得包括在内,但我不喜欢这种解决办法都没有。

I can't seem to get it to render properly, and it doesn't matter which script tag is first, that's the one it adds/keeps the tilde on. I've resorted to including the jquery script twice, the first one will have a tilde but the second one will get included, but I don't like that solution at all.

我与VS 2012的工作,这是一个.NET 4.5 MVC应用程序。从我的搜索,似乎这是剃刀V1一个已知的问题,但似乎他们所提供的解决方案并不适用于此。

I'm working with VS 2012, it's a .NET 4.5 MVC application. From my searches it seems this was a known issue for Razor v1, but the solutions they provide don't seem to apply here.

推荐答案

您应该使用的 @ Url.Content() 帮手(我们不是在ASP。 NET更多,TOTO)。

You should be using @Url.Content() helper (We're not in ASP.NET any more, toto).

<script src="@Url.Content("~/Scripts/jquery-2.0.3.min.js")"></script> <!-- etc. -->

另一种方法是使用 Microsoft.AspNet .Web.Optimization 包并创建软件包:

Another option is to use the Microsoft.AspNet.Web.Optimization package and create bundles:

** BundleConfig.cs

**BundleConfig.cs

public void RegisterBundles(BundleCollection bundles) { // ... bundles.Add(new ScriptBundle("~/js/site").Include( "~/Scripts/jquery-2.0.3.min.js", "~/Scripts/easing.js", "~/Scripts/bootstrap.js" )); // ... }

然后在页面中使用:

Then in your page use:

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

更多推荐

ASP.NET MVC视图渲染不正确的脚本标签

本文发布于:2023-11-12 09:36:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   不正确   脚本   标签   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!