在C#/.NET MVC应用程序中从另一个类调用函数
我试图在我的程序中为代码重用创建更好的关注点分离,这样我就不会有一个臃肿的控制器来完成所有这些不同的事情 例如,在我的应用程序中,我有一个用户配置文件,用户可以上传一个配置文件pic。如果他们没有自定义他们的个人资料图片,我会将默认的个人资料图片设置为头像。我通过一种方法来检查他们的配置文件pic字符串是否为空 我创建了一个名为在C#/.NET MVC应用程序中从另一个类调用函数,c#,asp.net-mvc,C#,Asp.net Mvc,我试图在我的程序中为代码重用创建更好的关注点分离,这样我就不会有一个臃肿的控制器来完成所有这些不同的事情 例如,在我的应用程序中,我有一个用户配置文件,用户可以上传一个配置文件pic。如果他们没有自定义他们的个人资料图片,我会将默认的个人资料图片设置为头像。我通过一种方法来检查他们的配置文件pic字符串是否为空 我创建了一个名为HelperMethods的文件夹,并创建了一个名为UserHelperMethods的类,该类当前有一个函数: namespace HiRatik.Stories.He
HelperMethods
的文件夹,并创建了一个名为UserHelperMethods
的类,该类当前有一个函数:
namespace HiRatik.Stories.HelperMethods
{
public class UserHelperMethods
{
//checks if the user's profile pic is null and sets it to default pic if it is
public string GetUserProfilePic(ApplicationUser user)
{
if (user.ProfilePic == null)
{
user.ProfilePic = "profile_pic_default.png";
}
return user.ProfilePic;
}
}
}
现在,在控制器的文件夹下,我使用HiRatik.Stories.HelperMethods添加了代码>
并尝试从UserController
调用公共函数GetUserProfilePic
。但我在实现上遇到了一个错误。我希望能够将许多与用户相关的通用函数放在另一个类中,如UserHelperMethods
,以清理控制器中的大量内容,但我缺少一些实现方面的内容。顶部的using语句是灰色的,因此它没有拾取函数调用。有什么想法吗
您需要将helper方法类的实例添加到要在其中使用它的每个类中
UserHelpMethods helper = new UserHelperMethods();
然后您可以将其用作:
helper.GetUserProfilePic(foundUser);
...
help.DoSomethingImportant(foundUser);
您需要将helper方法类的实例添加到要在其中使用它的每个类中
UserHelpMethods helper = new UserHelperMethods();
然后您可以将其用作:
helper.GetUserProfilePic(foundUser);
...
help.DoSomethingImportant(foundUser);
您可能希望将其作为一个扩展。然后你可以这样称呼它:
user.GetProfilePic();
您必须做的更改是,使您的类和方法都是静态的,并且在参数之前有this关键字。差不多
public static class ApplicationUserExtensions
{
//checks if the user's profile pic is null and sets it to default pic if it is
public static string GetProfilePic(this ApplicationUser user)
{
if (user.ProfilePic == null)
{
user.ProfilePic = "profile_pic_default.png";
}
return user.ProfilePic;
}
}
您可能希望将其作为一个扩展。然后你可以这样称呼它:
user.GetProfilePic();
您必须做的更改是,使您的类和方法都是静态的,并且在参数之前有this关键字。差不多
public static class ApplicationUserExtensions
{
//checks if the user's profile pic is null and sets it to default pic if it is
public static string GetProfilePic(this ApplicationUser user)
{
if (user.ProfilePic == null)
{
user.ProfilePic = "profile_pic_default.png";
}
return user.ProfilePic;
}
}
您可以将代码更新为以下内容之一
A-
在你的控制器里,你可以这样叫它
UserHelperMethods.Instance().GetUserProfilePic(founduser);
myInstance.FunctionName();
还是最简单的方法
var helperObj = new UserHelperMethods();
helperObj.GetUserProfilePic(founduser);
您不需要在diff控制器中始终创建实例的diff
我希望这对你有帮助 您可以将代码更新为以下内容之一
A-
在你的控制器里,你可以这样叫它
UserHelperMethods.Instance().GetUserProfilePic(founduser);
myInstance.FunctionName();
还是最简单的方法
var helperObj = new UserHelperMethods();
helperObj.GetUserProfilePic(founduser);
您不需要在diff控制器中始终创建实例的diff
我希望这对你有帮助 只需创建类的实例
var myInstance = new UserHelperMethods();
然后只需使用myInstance
对象访问UserHelpMethods
类中的函数
因此,您可以像这样调用UserHelpMethods
中的任何函数
UserHelperMethods.Instance().GetUserProfilePic(founduser);
myInstance.FunctionName();
所以在你的情况下
myInstance.GetUserProfilePic(foundUser);
只需创建类的实例
var myInstance = new UserHelperMethods();
然后只需使用myInstance
对象访问UserHelpMethods
类中的函数
因此,您可以像这样调用UserHelpMethods
中的任何函数
UserHelperMethods.Instance().GetUserProfilePic(founduser);
myInstance.FunctionName();
所以在你的情况下
myInstance.GetUserProfilePic(foundUser);
我会考虑使这些方法静态化。
namespace HiRatik.Stories.HelperMethods
{
public class UserHelperMethods
{
//checks if the user's profile pic is null and sets it to default pic if it is
public static string GetUserProfilePic(ApplicationUser user)
{
if (user.ProfilePic == null)
{
user.ProfilePic = "profile_pic_default.png";
}
return user.ProfilePic;
}
}
}
如果helper方法不依赖于UserHelperMethods
对象中的任何状态,这将使从任何位置调用方法变得更加容易,因为不再需要创建UserHelperMethods
类型的实例。您可以这样调用该方法
UserHelperMethods.GetUserProfilePic(foundUser);
我会考虑使这些方法静态化。
namespace HiRatik.Stories.HelperMethods
{
public class UserHelperMethods
{
//checks if the user's profile pic is null and sets it to default pic if it is
public static string GetUserProfilePic(ApplicationUser user)
{
if (user.ProfilePic == null)
{
user.ProfilePic = "profile_pic_default.png";
}
return user.ProfilePic;
}
}
}
如果helper方法不依赖于UserHelperMethods
对象中的任何状态,这将使从任何位置调用方法变得更加容易,因为不再需要创建UserHelperMethods
类型的实例。您可以这样调用该方法
UserHelperMethods.GetUserProfilePic(foundUser);
您的类中是否有该对象的实例UserHelperMethods helper=newuserhelpermethods()
。使用:helper.GetUserProfilePic(foundUser)
ah。嗯!!!谢谢。你们班上有那个对象的实例吗UserHelperMethods helper=newuserhelpermethods()
。使用:helper.GetUserProfilePic(foundUser)
ah。嗯!!!谢谢。根据您使用的C的版本,您可以编写整个方法,如return user.ProfilePic??“profile_pic_default.png”
根据您使用的C的版本,您可以编写整个方法,如return user.ProfilePic??“profile_pic_default.png”代码>