2

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

DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • 1
    Those don't sound like Virtual Directories but actual directories operating under a single domain. If that's the case then I think you are stuffed, if not then there shouldn't be a need to include the VirDir in the path as it's a reference to the client to retrieve the file not the server. Yes, you can pass Url helper output into Html helper where a path is required. For script references however, why not just do src="<%= helpermethod %>" in the script tag, I know the intellisense doesn't like it but it is valid. – Lazarus Feb 09 '10 at 13:53
  • Thanks for the comment Lazarus. I (in my somewhat limited knowledge of IIS) think that they are virtual directories, because that's how I've been creating them in IIS (right click website, new virtial directory, etc.). I'm going to explore the option of using helper methods. Hopefully that'll do the trick! – DaveDev Feb 09 '10 at 15:32

3 Answers3

1

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

Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79
  • 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 Feb 09 '10 at 14:54
  • Yeh i was stabbing i'll admit. I've been considering doing this for other reasons but i thought it might apply. – Matt Kocaj Feb 09 '10 at 15:51
1

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
  • 41,155
  • 72
  • 223
  • 385
1

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
  • 30,286
  • 23
  • 113
  • 184
  • 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 Feb 10 '10 at 16:59
  • What you have will work just as well, I only posted as an alternate solution for those who find this down the road. – Nate Feb 10 '10 at 17:29