在 ASP.NET MVC 视图中包含脚本引用的最佳方法是什么?

发布于 2024-10-06 00:47:11 字数 1172 浏览 11 评论 0原文

如您所知,ASP.NET MVC 将视图标记存储在 Views 目录中,该目录在层次结构上与 ASP.NET MVC Web 应用程序中使用的 URL 路由不兼容。另一方面,在 ASP.NET Web 窗体(以及 ASP.NET MVC 中)中,URL 可以并且通常确实具有嵌套的“目录”,或者更确切地说是路径分隔符,并且这与 Web 应用程序通常不具有嵌套的“目录”这一事实相结合。托管在 URL 的根路径中,而不是在子目录中,即“/stuff/here/MyActualApp”,因此有必要使用相对于应用程序根目录而不是相对于 URL 根目录的脚本路径。然而,与此同时,Visual Studio 脚本智能感知指示 URL 相对于正在编辑的文件进行映射。

此外,我在使用 runat="server" 来虚拟化根路径以支持“~/”时遇到了很多问题,例如 head 标签也需要 runat="server",这引入了各种的其他限制。

最后,还有一件事:如果除了智能感知就绪风格之外还引用了像 jQuery 这样的脚本的缩小风格,Visual Studio 将对此犹豫不决。所以你几乎必须使用转义代码来防止 VS 犹豫不决。

因此,自 VS 2005 以来,我一直在 Visual Studio 2010 中使用此语法或其变体,以便在 ASP.NET 视图标记中包含脚本,以处理 ASP.NET MVC 视图文件的嵌套文件夹的差异(不符合与实际的 URL 一起)以及需要使用 jQuery 的 vsdoc 风格而不是缩小版本,以便我可以进行智能感知。

<%if (false) { %>
<script src="../../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<% } %>
<%= "<script type=\"text/javascript\"" src=\"" 
  + ResolveUrl("~/Scripts/jquery-1.4.1.min.js") + "\"></script>"%>

除了使用CDN URL之外,还有比这更好的方法吗?太丑了。我希望微软现在就可以解决这个问题,而无需诉诸 ScriptManager 标签(这需要服务器端表单并使标记更加冗长)。

注意:我的问题不在于智能感知支持,而在于上面代码中的最后一行,必须发出一行而不是仅仅使用真正的标记。不过,我也希望智能感知支持做好准备。

As you know, ASP.NET MVC stores view markup in a Views directory, which is hierarchically incompatible with the URL routes that are used in an ASP.NET MVC web application. On the opposite end, in ASP.NET Web Forms (and in ASP.NET MVC, too), URLs can and usually do have nested "directories", or rather path separators, and this combined with the fact that web applications are often not hosted in the root path of a URL but rather in a sub-directory i.e. "/stuff/here/MyActualApp", it is necessary to use a script path relative to the root of the application rather than relative to the root of a URL. Meanwhile, however, Visual Studio script intellisense dictates that URLs map relatively to the file being edited.

Further, I've run into a lot of problems with using runat="server" to virtualize the root path to support "~/", such as the head tag needing to also be runat="server", and this introduces all kinds of other constraints.

Finally, one more thing: if the minified flavor of a script like jQuery is referenced in addition to the intellisense-ready flavor, Visual Studio will balk on it. So you almost have to use escaped code to keep VS from balking.

So I've been using this syntax, or variations of it, in Visual Studio 2010 since VS 2005 for including script in my ASP.NET view markup to deal with the discrepancies nested folders for ASP.NET MVC view files (which do not line up with actual URLs) as well as the need to use the vsdoc flavor of jQuery instead of the minified version so that I get intellisense working.

<%if (false) { %>
<script src="../../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<% } %>
<%= "<script type=\"text/javascript\"" src=\"" 
  + ResolveUrl("~/Scripts/jquery-1.4.1.min.js") + "\"></script>"%>

Aside from using a CDN URL, is there a better way than this? It's ugly. I wish Microsoft could have addressed this by now without resorting to ScriptManager tags (which require server-side forms as well as make the markup even more verbose).

Note: My issue is not with the Intellisense support so much as the last line in the code above, having to emit a line rather than just using real markup. However, I also want intellisense support readiness, too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不奢求什么 2024-10-13 00:47:11

我们使用 SquishIt 。它也组合并缩小了文件,并支持 css (甚至 点少)。

<head>
  <%= Bundle.Css()
    .Add("~/media/css/style.less")
    .Add("~/media/css/print.css")
    .Add("~/media/css/media.css")
    .Render("~/media/css/styles_#.css") %>
  <%= Bundle.JavaScript()
    .Add("~/media/js/jquery-1.4.3.js")
    .Add("~/media/js/jquery.equalHeights.js")
    .Add("~/media/js/jquery.cycle.lite.1.0.js")
    .Add("~/media/js/swfobject-2.2.js")
    .Add("~/media/js/site.js")
    .Render("~/media/js/js_#.js") %>
</head>

T4MVC 还可以提供一种引用 URL 的方法:

<script src="<%= Links.Scripts.Map_js %>" type="text/javascript"></script>

We use SquishIt. It combines and minifies the files, too, and supports css (and even dotLess).

<head>
  <%= Bundle.Css()
    .Add("~/media/css/style.less")
    .Add("~/media/css/print.css")
    .Add("~/media/css/media.css")
    .Render("~/media/css/styles_#.css") %>
  <%= Bundle.JavaScript()
    .Add("~/media/js/jquery-1.4.3.js")
    .Add("~/media/js/jquery.equalHeights.js")
    .Add("~/media/js/jquery.cycle.lite.1.0.js")
    .Add("~/media/js/swfobject-2.2.js")
    .Add("~/media/js/site.js")
    .Render("~/media/js/js_#.js") %>
</head>

T4MVC can also provide a way to reference the URL's:

<script src="<%= Links.Scripts.Map_js %>" type="text/javascript"></script>
花辞树 2024-10-13 00:47:11

<块引用>

如您所知,ASP.NET MVC 将视图标记存储在 Views 目录中,该目录在层次结构上与 ASP.NET MVC Web 应用程序中使用的 URL 路由不兼容

这是不正确的。您认为默认视图目录在哪些方面与路由不兼容?

<块引用>

相反,在 ASP.NET Web 窗体(以及 ASP.NET MVC 中)中,URL 可以并且通常确实具有嵌套的“目录”,或者更确切地说是路径分隔符,并且这与 Web 应用程序的事实相结合通常不托管在 URL 的根路径中,而是托管在子目录中,即“/stuff/here/MyActualApp”,因此有必要使用相对于应用程序根目录而不是相对于应用程序根目录的脚本路径一个网址

必须使用相对于应用程序根目录的脚本路径,而不是相对于 URL 根目录的脚本路径

root 或 Url 是什么意思?
MVC 默认情况下不使用嵌套目录(除非您将 RouteExistingFiles 设置为 true) URL 具有嵌套目录意味着什么? MVC 路由使用段而不是路径分隔符。

<块引用>

应用程序通常不托管在 URL 的根路径中,而是托管在子目录中
更准确地说,应用程序托管在不在根级别的 vdir 中。对于 MVC 3 及更低版本,您需要使用 @Url.Content 帮助器引用静态资源(css、javaScript)。

使用 MVC 4/Beta + Razor 及更高版本,您不再需要使用 @Url.Content 帮助器。

<块引用>

此外,我在使用 runat="server" 来虚拟化根路径以支持“~/”时遇到了很多问题,例如 head 标签也需要 runat="server",而这个引入了各种其他约束

您永远不应该将 runat="server" 与 MVC 应用程序一起使用来虚拟化根路径。

<块引用>

需要使用 jQuery 的 vsdoc 风格而不是缩小版本,以便我可以进行智能感知。
这又是不正确的。您永远不应该直接引用 vdoc 版本。 Visual Studio/VWD 检测何时存在 .vdoc 版本的脚本文件并启用智能。

您的帮助方法可能是最好的途径。您可以修改我的帮助程序方法 http://blogs.msdn.com/b/rickandy/archive/2011/05/21/using-cdns-to-improve-web-site-performance.aspx

通常人们会注释掉用于开发的 .min 参考,用于生产的开关注释以使用缩小版本。我们实际上正在研究一种使用缩小/捆绑来切换开发/生产的方法。请参阅 RequestReduce。和 http://weblogs.asp.net/scottgu/archive/2011/11/27/new-bundling-and-minification-support-asp-net-4-5-series.aspx

As you know, ASP.NET MVC stores view markup in a Views directory, which is hierarchically incompatible with the URL routes that are used in an ASP.NET MVC web application

That is incorrect. In what way do you think the default view directory is incompatible with routing?

On the opposite end, in ASP.NET Web Forms (and in ASP.NET MVC, too), URLs can and usually do have nested "directories", or rather path separators, and this combined with the fact that web applications are often not hosted in the root path of a URL but rather in a sub-directory i.e. "/stuff/here/MyActualApp", it is necessary to use a script path relative to the root of the application rather than relative to the root of a URL

it is necessary to use a script path relative to the root of the application rather than relative to the root of a URL

WHat does root or the Url mean?
MVC doesn't use nested directorys by default (unless you set RouteExistingFiles to true) What does it mean for a URL to have nested directories? MVC routing uses segments not path seperators.

applications are often not hosted in the root path of a URL but rather in a sub-directory
More correctly, apps are hosted in a vdir which is not at the root level. With MVC 3 and lower you need to reference static resources (css, javaScript) using the @Url.Content helper.

With MVC 4/Beta + Razor and higher, you no longer need to use the @Url.Content helper.

Further, I've run into a lot of problems with using runat="server" to virtualize the root path to support "~/", such as the head tag needing to also be runat="server", and this introduces all kinds of other constraints

You should never use runat="server" with a MVC application to virtualize the root path.

the need to use the vsdoc flavor of jQuery instead of the minified version so that I get intellisense working.
That again is incorrect. You should NEVER reference the vdoc versions directly. Visual Studio/VWD detects when a .vdoc version of a script file is present and enable intellesence.

Your helper approach is probably the best route. You could modify my helper approach http://blogs.msdn.com/b/rickandy/archive/2011/05/21/using-cdns-to-improve-web-site-performance.aspx

Typically folks comment out the .min references for development the switch comments for production to use the minified version. We are actually working on an approach to toggle development/production using minificatin/bundling. see RequestReduce. and http://weblogs.asp.net/scottgu/archive/2011/11/27/new-bundling-and-minification-support-asp-net-4-5-series.aspx

傲性难收 2024-10-13 00:47:11

你可以在开发过程中提供类似的东西,这样它就会向你展示智能传感器

<reference path="http://code.jquery.com/jquery-1.4.1.js"/>

you can give something like this during development , and so that it will show you the intellisens

<reference path="http://code.jquery.com/jquery-1.4.1.js"/>
澜川若宁 2024-10-13 00:47:11

在 ASP.NET MVC 4 中,Razor 已经变得足够智能,可以支持“~/”检测。希望 VS11 IntelliSense 工具能够跟上这一点。

<script src="~/Scripts/Controls.js"></script>

ASP.NET MVC4 中的新增功能:Razor 更改 (Alexander Beletsky)

同时,自从问题最初提出以来,Visual Studio 工具和 jQuery 源(因为问题中使用了 jQuery 作为参考)都进行了大量修改。 RickAndMSFT的答案当时并不适用。目前来看,他的答案是正确的;然而,由于这是一个不断变化的目标,看来到今年年底,人们将直接引用脚本,而 Razor 和修订后的 VS 工具将处理所有事情。

In ASP.NET MVC 4, Razor has become smart enough to support "~/" detection. Hopefully the VS11 IntelliSense tooling will keep up with this.

<script src="~/Scripts/Controls.js"></script>

New in ASP.NET MVC4: Razor changes (Alexander Beletsky)

Meanwhile, the Visual Studio tooling and the jQuery source (since jQuery was used for reference in the question) have both been heavily modified since the question was originally asked. RickAndMSFT's answer did not apply at that time. At the current time, his answer is correct; however, since this is a moving target it appears that by the end of this year one would just reference the script directly and Razor and revised VS tooling will take care of everything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文