【问题标题】:How to reference functions from other function files in ASP.NET Web Pages 2?如何从 ASP.NET Web Pages 2 中的其他函数文件中引用函数?
【发布时间】:2013-08-15 19:14:38
【问题描述】:

情况如下:

假设我在 App_Code 中有两个文件,每个文件都包含函数块:

这是文件 AppCode/File1.cshtml 中的示例函数 #1:

@functions {
  public static Boolean getTrue() {
    return true;
  }
}

这是 AppCode/File2.cshtml 文件中的示例函数 #2:

@functions {
  public static Boolean getFalse() {
    return false;
  }
}

我可以通过@File1.getTrue() 或@File2.getFalse() 从我的根文件夹中引用CSHTML 文件中的任一函数。

但是,我可以在 AppCode/File1.cshtml 中调用 @File2.getFalse() 以便:

@functions {
   public static Boolean getTrue() {
     return (!@File2.getFalse());
   }
}

【问题讨论】:

    标签: asp.net c#-4.0 razor razor-2 asp.net-webpages


    【解决方案1】:

    标有helper 关键字的函数中的代码是纯C#。因此,您应该删除 Razor @ 符号。这应该只用于将服务器端变量和表达式结果呈现给浏览器。

     @functions {
       public static Boolean getTrue() {
         return (!File2.getFalse());
       }
    }
    

    【讨论】:

    • 嗨,迈克,感谢您的回复。你是对的,@ 应该被排除在外。事实证明,问题在于 VS2012 中的 IntelliSense 没有拾取第二个函数文件,而是用红色波浪线在函数下划线。我误以为无法访问这些功能,但它正在编译和正常工作。
    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2023-02-03
    • 2015-10-06
    相关资源
    最近更新 更多