简体   繁体   中英

how to use an extension method from a code block in aspx page

I have an extension method which I can use from the .cs codebehind of an aspx page, but if I try to do it in a code block in the aspx, it can't find the extension method. Is there something I need to add to the page?

You need to include the namespace containing the extension method at the top of the page, like this:

<%@ Import Namespace="Your.Namespace" %>

You can also include it globally in Web.config:

<pages>
    <namespaces>
        <add namespace="Your.Namespace" />
    </namespaces>
</pages>

The appropriate using directive:

<%@ Import Namespace="NamespaceContainingTheStaticClass" %>

Or even better do it globally in web.config

<pages>
   <namespaces>
      <add namespace="NamespaceContainingTheStaticClass" />
   </namespaces>
</pages>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM