ASP.NET MVC视图渲染不正确的脚本标签 [英] ASP.NET MVC View not rendering script tags properly

查看:152
本文介绍了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视图渲染不正确的脚本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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