views:

232

answers:

2

Hey guys

I'm playing around with Razor + MVC 3 and have a really simple scenario... Basically I'm trying to create a very basic HTML helper but I'm getting the following exception:

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ScriptCss' and no extension method 'ScriptCss' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

The code for the extension looks like this:

    public static MvcHtmlString ScriptCss(this HtmlHelper htmlHelper, string path)
    {
        return MvcHtmlString.Create(String.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", path));
    }

Any ideas where I am going wroung?

Cheers Anthony

+5  A: 

I'd check a couple of things, are you

a) making sure the parent class of your extension method is public?

and b) import your respective namespace:

 @using MyNamespace;
Matthew Abbott
Hey guys... it turns out the namespaces section in the web.config isn't supported... which makes sense since its not using the asp.net view engine... Will be interesting to see how they handle doing the requirement in Razor
vdh_ant
The namespaces section in web.config is for configuring the ASPX parser and code generator. If we used it as-is in Razor we'd get a bunch of extra namespaces such as System.Web.UI imported, which are useless in Razor. We'll be adding a new web.config section for Razor in a future release. Until now, see Stacker's answer about CodeGeneratorSettings for global imports.
anurse
+3  A: 

According to answer in this post, http://forums.asp.net/p/1583383/3995794.aspx

you can use:

CodeGeneratorSettings.AddGlobalImport("MyNamespace");
stacker