开发者

Deploying ASP.NET MVC App to Multiple Virtual Directories (Issues with URL references & JS files)

开发者 https://www.devze.com 2022-12-19 10:28 出处:网络
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 ver

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. I开发者_运维知识库s 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


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


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消