Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define usings in MVC 3 Razor View Engine across entire site? [duplicate]

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?

like image 706
Dav Avatar asked Nov 29 '10 20:11

Dav


People also ask

What can the @page directive do in a Razor page?

@page makes the file into an MVC action, which means that it handles requests directly, without going through a controller. @page must be the first Razor directive on a page. @page affects the behavior of other Razor constructs.

What is the difference between Razor view and Razor page?

The difference between them is that View Pages are Razor views that are used to provide the HTML representations (aka views) for services in much the same way View Pages work for MVC Controllers.


1 Answers

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

like image 193
Paul Rowland Avatar answered Oct 13 '22 21:10

Paul Rowland