views:

367

answers:

3

I have implemented the Url Helper extensions that Kazi Manzur has suggested in his MVC best practices guide here

My Url Helper extension method to get a script file:

public const string ScriptDir = "~/Assets/Scripts";
public static string Script(this UrlHelper helper, string fileName)
{
    return helper.Content(string.Format("{0}/{1}", ScriptDir, fileName));
}

And in my Master page I simply add the jQuery script to my page like so:

<script type="text/javascript" src="<%= Url.Script("jquery-1.3.2.min.js") %>"></script>

How would I get intellisense working for jQuery since Visual Studio doesn't know at design time that jquery-1.3.2.min.js is included in the Master page?

The workaround that I am currently including the following code (hardcode my -vsdoc script location) in my Master page. This may be the best solution at the moment:

<% if (false) { %> <script type="text/javascript" src="~/Assets/Scripts/jquery-1.3.2-vsdoc.js"></script> <% } %>
+1  A: 

Simple:

/// <reference path="jquery-1.3.2-vsdoc.js" />

Or whatever the appropriate name is.

As an added bonus, you may want to use the following as a reference for VS Intellisense with Javascript.

Jordan S. Jones
That only works within other js files - it has no effect on aspx/ascx files.
Zhaph - Ben Duguid
Yes, unfortunately, I actually read the whole question *after* I posted my answer. Oh well.
Jordan S. Jones
+1  A: 

He suggests mapping those URLs to a helper method to avoid duplication. However, in the case of a master page, you already have a central place for that information to live (i.e. you are not repeating yourself).

So, in this case, I would suggest that in this circumstance that it is not only unnecessary, but that it is actively harmful. Intellisense is much more important in this case than slavishly being 100% consistent.

Travis
+5  A: 

You don't have to include the "-vsdoc" part of the filename. The vsdoc intellisense file will be picked up automatically. However, that basic workaround is the only way I've been able to get intellisense to work with jquery and a method for the script path. I also have to put the workaround markup in any .ascx controls or views that don't reference the master page for which I want intellisense.

The workaround I use, the same as yours minus the "-vsdoc":

<% if (false) { %>
    <script src="../../Content/scripts/jquery-1.3.2.js" type="text/javascript"></script> 
<% } %>

However, the workaround defeats the purpose of having the path method since you have to hard-code the path anyway. Not much of an answer - just a confirmation of the same issue.


If you enable compiling for views, the above code will cause an unreachable code warning. This code using directives doesn't cause a warning and won't be compiled to the view:

<% #if (false) %>
    <script src="../../Content/scripts/jquery-1.3.2.js" type="text/javascript"></script> 
<% #endif %>
Steven Lyons