如何从C#MVC中的控制器/模型调用外部函数?

如何从C#MVC中的控制器/模型调用外部函数?,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我是C#MVC的新手。我正在尝试创建一个类,在这个类中我可以放置所有可重用的函数,并且我希望能够从我的控制器和模型中调用这些函数。有可能吗 以下是我的项目文件夹结构: 所以我创建了一个Helpers文件夹,里面有一个commonFunctions类。 现在我有一个函数在里面 namespace myProject.Helpers { public class CommonFunctions { public static string GenerateSHA(s

我是C#MVC的新手。我正在尝试创建一个类,在这个类中我可以放置所有可重用的函数,并且我希望能够从我的控制器和模型中调用这些函数。有可能吗

以下是我的项目文件夹结构:

所以我创建了一个Helpers文件夹,里面有一个commonFunctions类。 现在我有一个函数在里面

namespace myProject.Helpers
{
    public class CommonFunctions
    {
        public static string GenerateSHA(string sString)
        {
            string result = "";
            SHA1 sha = new SHA1CryptoServiceProvider();
            byte[] password = sha.ComputeHash(Encoding.Default.GetBytes(sString));
            result = Encoding.Default.GetString(password);
            return result;
        }
    }
}
我想从我的模型中调用这个函数。我怎样才能做到


提前谢谢

您可以像这样轻松调用该函数:

var sha = Helpers.CommonFunctions.GenerateSHA("Your String");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace myProject.Models
{
    public class YourViewModel
    {
        public YourViewModel()
        {

             //This is the constructor of the class
            //Call the function you need
           var tVar = Helpers.CommonFunctions.GenerateSHA("String to process");

        }
    }
}
或者,您可以使用语句添加
,每次调用不带命名空间规范的函数:

using myProject.Helpers;

// And somewhere in your classes
var sha = CommonFunctions.GenerateSHA("Your String");

这是一个像这样的直接呼叫:

var sha = Helpers.CommonFunctions.GenerateSHA("Your String");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace myProject.Models
{
    public class YourViewModel
    {
        public YourViewModel()
        {

             //This is the constructor of the class
            //Call the function you need
           var tVar = Helpers.CommonFunctions.GenerateSHA("String to process");

        }
    }
}

它不起作用。我得到错误:名称空间“myProject”中不存在类型或名称空间名称“Helpers”(是否缺少程序集引用?)。如果您的模型位于不同的项目中,则需要添加对此部件的引用。此外,“myProject”似乎只是一个名字,你用它来不透露你真正的项目名称。请在复制粘贴一些代码表单时将其更改回此处它显示了什么?!命名空间“myProject”中不存在类型或命名空间名称“Helpers”(是否缺少程序集引用?)(您的ViewModel)应添加到(myProject)中的(Models)文件夹中CommonFunctions文件的BuildAction是否设置为“Compile”?