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

.Netasp.net Mvc

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

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)

Solution 2 - .Net

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;

Solution 3 - .Net

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

Solution 4 - .Net

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.

Solution 5 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajan RawalView Question on Stackoverflow
Solution 1 - .NetShyjuView Answer on Stackoverflow
Solution 2 - .NetEric J.View Answer on Stackoverflow
Solution 3 - .NetRishikeshView Answer on Stackoverflow
Solution 4 - .NetKavis ShawView Answer on Stackoverflow
Solution 5 - .NetFadeloveskyView Answer on Stackoverflow