c#razor在外部文件中起作用

时间:2016-01-04 20:38:59

标签: c# html function razor

我的cshtml页面上有一个函数,用于检查输入的电子邮件是否有效,它是否正常工作,但是我现在需要将其移动到外部文件中,我创建了所有其他函数,我不能似乎找到关于此的任何信息,任何人都可以告诉我该文件应该具有什么文件扩展名?我知道它需要进入App_Code文件夹,我应该对我的功能做些什么特别的事情吗?以及如何在我的网页上调用它?我的讲师从字面上没有说明这一点。

bool IsEmail(string email) {
    //declare the return variable 
    bool rst = false;

    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
    // does the string follow the regex structure?
    System.Text.RegularExpressions.Match match = regex.Match(email);
    //if yes
    if (match.Success){
        rst = true;
    } else {
        rst = false;   
    }

    return rst; 
}

提前致谢

1 个答案:

答案 0 :(得分:2)

App_Code中的新文件中:

public static class GodObject {
    public static bool IsEmail(string email) {
        //declare the return variable 
        bool rst = false;

        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
        // does the string follow the regex structure?
        System.Text.RegularExpressions.Match match = regex.Match(email);
        //if yes
        if (match.Success){
            rst = true;
        } else {
            rst = false;   
        }

        return rst; 
    }
}

然后您可以从几乎任何地方引用GodObject.IsEmail(...)

这可能是你的教授所追求的,然而,有更好的方法来进行这种验证。