Extension Method and Razor Page

前端 未结 3 1483
抹茶落季
抹茶落季 2021-01-13 14:39

I have defined an extension method in app_code like below.

public static class Extensions
{
    public static string Hi(this object obj)
    {
        return         


        
相关标签:
3条回答
  • 2021-01-13 14:56

    Just like a normal scenario you have to include it in a using. the razor syntaz is

    @using Namespace.Namespace
    
    0 讨论(0)
  • 2021-01-13 14:59

    C# only allows you to call extension methods qualified by an object instance.
    If you have an extension method that extends your type, you can't call it "directly"; you need to write this.ExtensionMethod().

    The only way to do what you're asking for is to make a class which inherits WebPage (or WebViewPage for MVC views) and change your Razor page to inherit that class (using the @inherits directive)

    0 讨论(0)
  • 2021-01-13 15:00

    I don't think you can call just @Hi(). Pretty sure is has to be @this.Hi()

    0 讨论(0)
提交回复
热议问题