0

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>

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.

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.

4

2 回答 2

2

您应该使用@Url.Content()帮助程序(我们不再使用 ASP.NET,toto)。

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

另一种选择是使用Microsoft.AspNet.Web.Optimization包并创建捆绑包:

**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"
  ));
  // ...
}

然后在您的页面中使用:

@Scripts.Render("~/js/site")
于 2013-09-25T18:14:24.257 回答
1

将引用粘贴在捆绑包中并使用@Scripts.Render("~/bundlename")或使用

<script src="@Url.Content("~/scripts/jquery-2.0.3.min.js")"></script>
于 2013-09-25T18:14:17.833 回答