views:

140

answers:

2

I am using the latest version of MVCContrib and attempting to include a stylesheet via the following helper method:

<%=Html.Stylesheet(Links.Content.Site_css)%>

The path rendered is incorrectly calculated as:

<link type="text/css" rel="stylesheet" href="/content/css/Content/Site.css" />

The actual path should be: /Content/Site.css

Is this a bug?

The following methods work correctly:

METHOD 1: <%=Html.Stylesheet("~/Content/Site.css")%>

METHOD 2: <link type="text/css" rel="stylesheet" href="<%:Links.Content.Site_css %>" />

UPDATE 1:

Links.Content.Site_css represents a static field that is auto-generated using T4MVC

UPDATE 2: Here is what the code generated by T4MVC looks like...

namespace Links {

    ...snipped for brevity...

    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public static class Content {
        private const string URLPATH = "~/Content";
        public static string Url() { return T4MVCHelpers.ProcessVirtualPath(URLPATH); }
        public static string Url(string fileName) { return T4MVCHelpers.ProcessVirtualPath(URLPATH + "/" + fileName); }

        public static readonly string Site_css = Url("Site.css");
}
+2  A: 

Not a bug. This works as expected. Refer to Using MvcContrib ScriptInclude, Stylesheet, And T4MVC

the output of the T4MVC Links are relative paths that have been resolved (meaning they no longer have the “~”). The MVC Contrib Helpers assume that if the URL passed it it doesn’t have the “~”, then it will prepend either “~/Scripts/” for scripts or “~/content/css/'” for styles. Seeing that I have moved my scripts, styles, and images under the “~/Content” folder, there are a couple of changes to the MVC Contrib Html Helpers that could make this work… Provide some sort of mechanism to define the paths prepended to the Scripts and Styles if there is no “~” Override the Html Helpers with another option to not prepend any path information Possibly search for the “/” instead of the “~” when determining if a path should be prepended to the URL

Ahmad
Thanks for the response. As you mentioned, this issue also occurs with the Html.ScriptInclude() helper. This seems like a mismatch between MVCContrib and T4MVC (part of MVCContrib) which deserves some consideration in future release.
Rokal
A: 

I do not think this works as expected. Prefixing "~" only works when your site is residing in say for exampel localhost/ . I have a site residing in localhost/MySite and my links become href="/Mysite/Mysite/Content/Css/Layout.css". Know how to fix this?

Johan Nilsson