在 ASP.NET MVC 中,我可以在哪里放置自定义类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11515973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:32:18  来源:igfitidea点击:

Where can I put custom classes in ASP.NET MVC?

.netasp.net-mvc

提问by Rajan Rawal

I have some utility functions and Pagination function. I want to create classes named Utility and Pagination for these functions respectively, so that I can use these class function in more than one controllers.

我有一些实用功能和分页功能。我想分别为这些函数创建名为 Utility 和 Pagination 的类,以便我可以在多个控制器中使用这些类函数。

So where can I put these class in my folder structure, and how can I access then?

那么我可以将这些类放在我的文件夹结构中的什么位置,然后我该如何访问呢?

回答by Shyju

You can either create a new Folder called Helpersunder the root and keep your classes physically there. I would keep my classes under a different namespace called Helpers

您可以Helpers在根目录下创建一个名为的新文件夹,并将您的类物理保存在那里。我会把我的类放在一个不同的命名空间下,称为Helpers

namespace MyProject.Helpers
{
  public class CustomerHelper
  {
        //Do your class stuff here
  }
}

To accees this in my other classes (Ex : Controllers) ,I can either use the fully qualified name

为了在我的其他类(例如:Controllers)中加入这个,我可以使用完全限定的名称

var custHelper=new MyProject.Helpers.CustomerHelper(); 

OR

或者

add a Importstatement at the top so that i can skip the fully qualified name

Import在顶部添加一条语句,以便我可以跳过完全限定名称

//Other existing Import statements here
using MyProject.Helpers;
public class RackController : Controller
{
  public ActionResult Index()
  {
     var custHelper=new CustomerHelper(); 
     //do something with this  
     return View();    
  } 
}

If you think your Helper method can be used in another project also, You may consider to keep them physically in a separate project(of type class library). To use this in your project, Add a reference to this project and use it like what we did above (use either fully qualified name or use import statement)

如果您认为您的 Helper 方法也可以在另一个项目中使用,您可以考虑将它们物理地保存在一个单独的项目中(类型类库)。要在您的项目中使用它,请添加对此项目的引用并像我们上面所做的那样使用它(使用完全限定名称或使用导入语句)

回答by Eric J.

You can put your helper classes anywhere you find logical and convenient.

你可以把你的助手类放在任何你觉得合乎逻辑和方便的地方。

Personally I create a folder Helpersoff of the main project folder.

我个人Helpers从主项目文件夹中创建了一个文件夹。

You can use them anywhere, either by fully qualifying the class name or with a usingstatement.

您可以在任何地方使用它们,通过完全限定类名或使用using语句。

In a Razor view, you would use

在 Razor 视图中,您将使用

@using MyProject.Helpers

In a controller or model you would use

在您将使用的控制器或模型中

using MyProject.Helpers;

回答by Rishikesh

Another approach could be creating a Base Controller class and adding your common logic there, and deriving your controller from your base controller.

另一种方法可能是创建一个基本控制器类并在那里添加您的通用逻辑,然后从您的基本控制器派生您的控制器。

public class MyBaseController:Controller
{
   public void CommonFunction()
    {
    }
}

Use like...

使用像...

public HomeController:MyBaseController
{
}

回答by Kavis Shaw

You can create a new project,and then put the common classes into the project . If you want to use them,just Add Reference the project.

您可以创建一个新项目,然后将公共类放入项目中。如果你想使用它们,只需添加引用项目。

回答by user2874877

Also, Please be sure to set the "Build Action" of your Class to "Compile". In Visual Studio 2015, By default, every Class created within the App_Code (by right click -> Add -> Class) got "Content" as Build Action.

另外,请务必将您的类的“构建操作”设置为“编译”。在 Visual Studio 2015 中,默认情况下,在 App_Code(通过右键单击 -> 添加 -> 类)中创建的每个类都将“内容”作为构建操作。