views:

126

answers:

3

Hi Guys

I'm not sure if I have my question named correctly but here's the issue:

I have an MVC.NET app that is deployed at multiple virtual directories. The reason for this is because we have different versions under various stages of QA.

The problem with this is that in one site the .js references are for

/VirtualDir/scripts/file.js

and on another site it's

/OtherVirtualDir/scripts/file.js

Another aspect of this issue is that I have HtmlHelpers that create links. The problem with this is that I don't have a UrlHelper in the HtmlHelper methods. Is it 'ok' to pass the UrlHelper from the view into the HtmlHelper? Can I get this to generate the tags?

Has anyone else had to deal with these kinds of issues? Any suggestions?

Thanks

Dave

+1  A: 

You might consider creating controllers to return FileResult's for static content (css, js, etc) that encapsulates the paths.

cottsak
I didn't consider that, and I'll admit it struck me as 'not-a-bad-idea' as I was reading it, but then it occurred to me 'do my controllers care about files?'. Hmm... I'll think about this one a little longer and see.-thanks
DaveDev
Yeh i was stabbing i'll admit. I've been considering doing this for other reasons but i thought it might apply.
cottsak
+1  A: 

Ok, in the end it turned out the solution is this:

<%= Html.JavaScriptTag("siteFunctions.js")%>
<%= Html.MyCSS("Site.css")%>
<%= Html.MyImage("loading_large.gif") %>

with the following definition for the first example above

public static string JavaScriptTag(this HtmlHelper htmlHelper, string fileName)
{
    TagBuilder js = new TagBuilder("script");
    string path = VirtualPathUtility.ToAbsolute("~/Scripts/" + fileName);
    js.MergeAttribute("src", path);
    js.MergeAttribute("type", "text/javascript");

    return js.ToString();
}

these HtmlHelpers are fantastic!

DaveDev
+1  A: 

When making references to files from a View in MVC, I always use this

<script src="<%= Url.Content ("~/Scripts/jquery-1.3.2.js") %>" type="text/javascript"></script>

Since View refferences are relative to the resource requesting them, using this bit of Javascript in your master page can help if you need "relative" paths inside your .JS files.

<script type="text/javascript">var imgRoot = '<% = Page.ResolveUrl("~/Images/") %>';</script>

And finally, CSS works just fine, since all paths in CSS are relative to the location of the .css file.

Nate Bross
good answer.. I didn't know about Url.Content(). It makes sense. I think I'll stick with the Html.MyJavaScript() solution though becuase 1. it allows for cleaner code, and 2. i'm too lazy to change it!! :-)
DaveDev
What you have will work just as well, I only posted as an alternate solution for those who find this down the road.
Nate Bross