我在哪里可以把自定义类的ASP.NET MVC? [英] Where can I put custom classes in ASP.NET MVC?
问题描述
我有一些实用的功能和分页功能。我想创建分别取名工具和分页这些功能类,这样我可以在一个以上的控制器使用这些类的功能。
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.
所以,我在哪里可以把这些类在我的文件夹结构,以及如何访问呢?
So where can I put these class in my folder structure, and how can I access then?
推荐答案
您可以创建助手
在根目录下的新文件夹名为,并保持你的类身体出现。我会保持我的课在一个叫不同的命名空间助手
You can either create a new Folder called Helpers
under the root and keep your classes physically there. I would keep my classes under a different namespace called Helpers
namespace MyProject.Helpers
{
public class CustomerHelper
{
//Do your class stuff here
}
}
要accees这在我的其他类(例如:控制器),我既可以使用完全合格的名称
To accees this in my other classes (Ex : Controllers) ,I can either use the fully qualified name
var custHelper=new MyProject.Helpers.CustomerHelper();
或
在顶部添加导入
语句,这样我可以跳过完全合格的名称
add a Import
statement at the top so that i can skip the fully qualified name
//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();
}
}
如果你认为你的助手方法可以用于另一个项目也可以考虑让他们的身在一个单独的项目(类型类库)。要在项目中使用这一点,添加一个指向该项目,并使用它就像我们上面做了(无论是使用完全合格的域名或使用import语句)
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)
这篇关于我在哪里可以把自定义类的ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!