70

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62

5 Answers5

87

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)

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 7
    @ppumkin - there are no coding practices in asp.net mvc that say you can't create your own folders or namespaces. So no, it's not in-line with any coding practices, but it's not out of line with them. It's just something that is in your own control. – Erik Funkenbusch May 19 '13 at 07:08
  • 1
    Yea I ended up using the folder Helpers and they get reused everywhere now. So that was some good advice. And you are right @MystereMan there is not really a documented proper way but many good suggestion like this. Thanks – Piotr Kula May 20 '13 at 07:44
  • I used the "physically separate project" but I can't find my Class Library .config file in my main MVC application. Where can I find the settings file, I only get the .dll in \bin folder. – Iztoksson Aug 14 '15 at 08:20
  • So where would the classes go that are created and don't fit under Models, or utility class. For ex, I have a class that's main function is to store two different data structures is returned by a method. – eaglei22 Aug 23 '18 at 17:19
  • `Helpers` or `Utilities` etc. Again, there are no golden rules. Follow what make more sense to your team. IMHO, Consistency is important than names. – Shyju Aug 23 '18 at 17:25
  • Between `var custHelper=new MyProject.Helpers.CustomerHelper();` and importing, is either one programatically quicker than the other? – Mitchell May 22 '20 at 20:55
  • @Shyju why your helper is not static?? – AminM Dec 02 '20 at 16:45
12

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

Personally I create a folder Helpers off of the main project folder.

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

In a Razor view, you would use

@using MyProject.Helpers

In a controller or model you would use

using MyProject.Helpers;
Eric J.
  • 147,927
  • 63
  • 340
  • 553
7

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
{
}
Rishikesh
  • 486
  • 6
  • 15
6

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.

Kavis Shaw
  • 61
  • 2
1

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.

Fadelovesky
  • 121
  • 2
  • 3