Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

.net - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
  }
}

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

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

OR

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();    
  } 
}

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...