asp.net – 在几个网页(.cshtml文件)中使用Razor @functions

我有以下功能,我想要在我的asp.net网页2应用程序中的几个.cshtml视图.如何使这个函数可用于应用程序中的任何视图(而不只是一个).
@functions {

    public bool DisplayButton(String startDate,String endDate)
    {
        return Convert.ToDateTime(startDate) < DateTime.Now && Convert.ToDateTime(endDate) > DateTime.Now;
    }
}

解决方法

在App_Code中创建一个名为Functions.cshtml的文件,然后将您所拥有的代码粘贴到文件中.然后,您可以在任何.cshtml文件中使用文件名前缀DisplayButton方法:
var myBool = Functions.DisplayButton(DateTime.Now,DateTime.Now.AddDays(30));

有关在ASP.NET网页中使用函数和帮助的更多信息,请阅读:http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix

相关文章

判断URL文件是不是在于在。private static bool UrlIsExist(...
由于在.net中,Request时出现有HTML或Javascript等字符串时,...
public static bool ProcessIdCard(this string idCard, out...
protected void Page_Load(object sender, EventArgs e){ Sc...