views:

2599

answers:

2

I have an ASP.NET MVC app which runs fine under the debugger running from http://localhost:9002/ But when I publish it to http://localhost/Zot/ the calls to Url.Content are returning incorrect values.

I have script tags like

<script src="<%= Url.Content("~/Scripts/util.js") %>" ...

In the published site this produces:

<script src="Zot/Scripts/util.js" ...

Instead of

<script src="/Zot/Scripts/util.js" ...

or

<script src="Scripts/util.js" ...

I have stylesheet tags like:

<link href="~/Content/Site.css" runat="server" ...

That produce the right thing:

<link href="Content/Site.css" ...

Any suggestions on why Url.Content is failing. I clearly can't put a runat="server" on the <script> tags.

+4  A: 

I tend to use Rob Conery's Script Registration helper:

public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, string scriptLib) {
  //get the directory where the scripts are
  string scriptRoot = VirtualPathUtility.ToAbsolute("~/Scripts");
  string scriptFormat="<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";
  return string.Format(scriptFormat,scriptRoot,scriptLib);
}

Usage:

<%= Html.RegisterJS("myscriptFile.js") %>

As you can see in the example, this uses VirtualPathUtility to resolve the url of the Scripts directory. This should also help to bypass the problem of tag soup.

Dan Atkinson
Thanks! That worked great.
Rob Walker
Rob, please also note Levi's response, as apparently this was a bug that was fixed in RC2.Cheers.
Dan Atkinson
+1  A: 

This should have been fixed in RC2. If you are using RC2 and are still seeing this problem, please file a bug at http://forums.asp.net/1146.aspx.

Levi