11

I wrote a simple extension method for UrlHelper:

public static class ExtensionMethods
{
    private const string ImagesFolder = "~/Images";

    public static string Images(this UrlHelper url)
    {
        return url.Content(ImagesFolder);
    }
}

The above code resides in /Helper/ExtensionMethods.cs. It works just fine but I need to add using MyNamespace.Helper; in every cshtml where I want to use the Url.Images(). I the old days we would add another line to web.config:

<system.web>
    <pages>
        <namespaces>
            <add namespace="MyNamespace.Helper"/>
        </namespaces>
    </pages>
</system.web>

But the above does not seem to be picked up by Razor. I tried adding my using statement to _ViewStart.cshtml, with the same result.

So, what's Razor's way of specifying a using across the entire site?

Dav
  • 1,102
  • 3
  • 10
  • 22
  • 1
    Definitely a dupe - just didn't come across it in my searches. Thanks for looking :-) – Dav Nov 29 '10 at 23:55

1 Answers1

8

As the accepted linked answer suggests you can add "using" to all views by adding to section of config file.

For a particular view you can just use

@using MyNamespace.Helper

Paul Rowland
  • 8,244
  • 12
  • 55
  • 76