35

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
  • 27,703
  • 6
  • 70
  • 62
pupeno
  • 267,428
  • 120
  • 345
  • 578
  • Is this 'accessing' or 'referencing'? [not picking nits - a question of transitioning from vb] – justSteve Jul 12 '09 at 14:46
  • @justSteve what's the difference? – pupeno Jul 12 '09 at 17:25
  • In my mind....referencing is the act of prepping the system for the actual utilization (accessing). We can't use a method of a different class until we've setup the reference. – justSteve Jul 13 '09 at 01:00

2 Answers2

49

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
  • 27,703
  • 6
  • 70
  • 62
18

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

@using MyBlogEngine;  
p.campbell
  • 95,348
  • 63
  • 249
  • 319