开发者

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

开发者 https://www.devze.com 2023-01-27 17:24 出处:网络
This question already has answers here: Razor HtmlHelper Extensions (or other namespaces for views) Not Found
This question already has answers here: Razor HtmlHelper Extensions (or other namespaces for views) Not Found (8 answers) Closed 6 years ago.

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:

<syst开发者_高级运维em.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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消