views:

162

answers:

2

I'm in the process of deploying an ASP.NET MVC app to IIS 6, but am running into an issue with the root path.

In Global.asax, I have the root path mapped:

routes.MapRoute("Root", "", 
    new { controller = "Dashboard", action = "Index", id = "" });

When I navigate to http://servername:70/test2/, the app displays the right page, but the stylesheet and JavaScript files are not loading. Looking at the source, the paths are showing like this:

<script src="test2/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" 
    href="test2/Content/stylesheets/app.css" />

Which makes the browser look for

http://servername:70/test2/test2/Content/stylesheets/app.css

When I go directly to a controller (http://servername:70/test2/Dashboard.aspx), the paths are correct:

<link rel="stylesheet" type="text/css" href="Content/stylesheets/app.css" />

This is also occurring with any links generated with ActionLink. The stylesheet and script paths are being generated with Url.Content:

<link rel="stylesheet" type="text/css" 
    href="<%= Url.Content("~/Content/stylesheets/app.css") %>" />
A: 

I have recently answered a question similar to this which uses Rob Conery's Script Registration Helper. I'll copy the answer in here for you, and add an example HtmlHelper for stylesheets.

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);
}

public static string RegisterCSS(this System.Web.Mvc.HtmlHelper helper, string styleLink, string rel) {
  //get the directory where the css is
  string stylesheetRoot = VirtualPathUtility.ToAbsolute("~/Content/Stylesheets");
  string styleFormat="<link type='text/css' href='{0}/{1}' rel='{1}' />\r\n";
  return string.Format(styleFormat, stylesheetRoot, styleLink, rel);
}

Usage:

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

Hope this helps.

Also, I note that there was another response on that question by Levi:

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.

If this is your preferred answer, the please upvote Levi's response.

Dan Atkinson
A: 

Or, use the Url.Content ...

<link href="<%=Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />

Brett Rigby