Can I use Extension Methods inline in an ASPX page?

Is it possible to do something like this inline in an ASPX page?

<%= Me.SomeExtensionMethod() %>

I can’t seem to figure out how to get this to work properly. I’m receiving an error saying that “SomeExtensionMethod” is not a member of the current Page object. I’ve added the necessary <%@ Import Namespace="..." %> directive at the top of my page. This Does work in code-behind.

This isn’t vitally important, but it would be good to know how to do in the future.

Thanks!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Adding imports in the namespace works for me!

<%@ Import Namespace="Foo.FooFoo" %>

Method 2

Try closing the .aspx page and opening it up again as per this answer. If that improves things at all (e.g. enable intellisense) but doesn’t solve it, please post any new errors you get.

You could also add the Public modifier to your Module or class definition. If you’re using Modules, it really doesn’t make sense to me that it would be required, but some discussion on this forum indicates that it might help.

Method 3

If it’s working in the codebehind, add the namespace to the function call:

<%=MyNamespace.ExtensionFcn("hello, world") %>

I’d do this before I’d modify the web.config.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x