简体   繁体   English

ASP.NET MVC相对路径而不会破坏JavaScript IntelliSense?

[英]ASP.NET MVC relative paths without breaking JavaScript IntelliSense?

Adding a script to a view generally involves something like this: 向视图添加脚本通常涉及以下内容:

<script src="../../Scripts/jquery-1.3.1.min.js" type="text/javascript"></script>

Unfortunately, this doesn't work if the app is deployed in a virtual directory under IIS 6. The alternatives discussed here involve using Url.Content with "~" to resolve the path dynamically, but this completely breaks JS IntelliSense. 不幸的是,如果将应用程序部署在IIS 6下的虚拟目录中,则此方法不起作用。 这里讨论的替代方案包括使用带有“〜”的Url.Content动态解析路径,但这完全破坏了JS IntelliSense。

Is there a way to get around this and make IntelliSense work without losing the ability to deploy the app in a virtual directory? 有没有办法解决这个问题,并使IntelliSense正常工作,而又不会失去在虚拟目录中部署应用程序的能力?

For deployment code you can use the google ajax apis to load JQuery. 对于部署代码,您可以使用google ajax api加载JQuery。 It is recommended because it help the page load time due to the use of the google CDN. 建议使用它,因为它可以帮助您使用Google CDN来加载页面。

to get intellisense add this to your page 要获得智能感知,请将其添加到您的页面

<% if(false){ %>
    <script src="../../Scripts/jquery-1.3.1.min.js" type="text/javascript"></script>
<%}%>

this will not be emitted because it is within an if(false) but intellisense will recognize it 这不会被发出,因为它在if(false)内,但是智能感知会识别它

In VS2010, one way to reference the "*vsdoc.js file in a virtual folder/directory and get Intellisense to work properly in a view without using a CDN is to simply use localhost in the address. A virtual directory is essential when centralizing script and content files for multiple web applications. In the example below, I created the "Shared" virtual directory in IIS 7.5. Hope this helps someone. 在VS2010中,引用虚拟文件夹/目录中的* vsdoc.js文件并使Intellisense无需使用CDN即可在视图中正常工作的一种方法是简单地在地址中使用localhost。虚拟目录对于集中化脚本至关重要和用于多个Web应用程序的内容文件。在下面的示例中,我在IIS 7.5中创建了“共享”虚拟目录,希望对您有所帮助。

@if (false)
{
   <script src="http://localhost/Shared/jQuery/js/jquery-1.7.2-vsdoc.js" type="text/javascript"></script>
}

I use something like this: 我用这样的东西:

   <script src="<%= ResolveUrl("~/Content/jquery-1.2.6.js") %>" type="text/javascript"></script>   

   <%--<script src="../../Content/jquery-1.2.6.js" type="text/javascript"></script>--%> 

You then have to uncomment the second reference when you want to use intellisense. 然后,当您要使用智能感知时,您必须取消注释第二个引用。 It's annoying, but the only workaround I've encountered. 这很烦人,但我遇到的唯一解决方法。

Check out the JScript IntelliSense FAQ on the Visual Web Developer Team Blog . Visual Web Developer Team Blog上查看JScript IntelliSense FAQ The comments also reference Srully's if(false) trick. 这些注释还引用了Srully的if(false)技巧。

I have created a HtmlHelper extension (PathReference is a JetBrains.Annotations attribute for ReSharper and can be omitted): 我创建了一个HtmlHelper扩展(PathReference是ReSharper的JetBrains.Annotations属性,可以省略):

public static class HtmlHelperExtensions
{
    public static MvcHtmlString Script(this HtmlHelper html, [PathReference]string scriptFile)
    {
        var filePath = VirtualPathUtility.ToAbsolute(scriptFile);
        return new MvcHtmlString("<script type=\"text/javascript\" src=\"" + filePath + "\"></script>");
    }
}

I then do like this in my master page 然后,我在母版页中这样做

<%
    if (false)
    {
%>
    <script src="../../Scripts/jquery-ui-1.8.9.custom.min.js" type="text/javascript"></script&gt;
<%
    }
%>
<%:Html.Script("~/Scripts/jquery-ui-1.8.9.custom.min.js")%>

I now have both intellisense and correct runtime references. 我现在同时具有智能感知和正确的运行时引用。

(Thanks to Sruly for the if(false) trick) (感谢Sruly的if(false)技巧)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net MVC 4是否有一些标准化的方式来引用JavaScript中的相对路径? - Does ASP.net MVC 4 have some standardized way of referring to relative paths in JavaScript? Javascript文件中的相对图像URL - ASP.net MVC和IIS 7 - Relative Image URL in Javascript File - ASP.net MVC and IIS 7 ASP.Net Usercontrols中的Javascript Intellisense - Javascript Intellisense in ASP.Net Usercontrols ASP.NET MVC3和JavaScript:如何获取其他JS文件中的函数和变量的Intellisense - ASP.NET MVC3 & JavaScript: How to get Intellisense on functions & variables located in other JS files Telerik UI Asp.net MVC在cshtml中缺少Javascript Intellisense(Kendo) - Missing Javascript Intellisense inside cshtml for Telerik UI Asp.net MVC (Kendo) Visual Studio Asp.net MVC,主文件中包含的javascript智能 - Visual studio Asp.net mvc, intellisense for javascript included in master file 这是使ASP.NET MVC和javascript intellisense发挥出色的“正确”解决方案吗? - Is this the “correct” solution for getting ASP.NET MVC and javascript intellisense to play nice? ASP.NET MVC和JavaScript - ASP.NET MVC and javascript 在ASP.NET MVC应用程序中引用Javascript文件中的路径的正确方法 - Correct way to referencing paths in a Javascript file in an ASP.NET MVC app 空ASP.Net Core项目中的JavaScript Intellisense - JavaScript Intellisense in Empty ASP.Net Core Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM