37

How do I access an extension method in an ASP.Net MVC View? In C# I do

using MyProject.Extensions;

and I remember seeing an XML equivalent to put in a view, but I can't find it anymore.

eu-ge-ne
  • 28,023
  • 6
  • 71
  • 62
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

2 Answers2

50

In View:

<%@ Import Namespace="MyProject.Extensions" %>

Or in web.config (for all Views):

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Linq" />
    <add namespace="System.Collections.Generic" />

    <add namespace="MyProject.Extensions" />
  </namespaces>
</pages>
eu-ge-ne
  • 28,023
  • 6
  • 71
  • 62
20

For pages using Razor / WebPages, you can include a using directive in your .cshtml page.

@using MyBlogEngine;  
p.campbell
  • 98,673
  • 67
  • 256
  • 322