Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 925
Rajan Rawal Avatar asked Jul 17 '12 04:07

Rajan Rawal


People also ask

How do I add a class to my controller?

Creating a Controller Class Follow these steps: Right-click the Controllers folder and select the menu option Add, New Item and select the Class template (see Figure 4). Name the new class PersonController. cs and click the Add button.

How do you use a class in razor view?

Just use a view model. If you are, you can make List<ObjectData> part of it. In your controller, you load up that list (lets call it ObjectDataList ), and send it to your view.

How do you add class to a model?

Adding Model Classes In Solution Explorer, right click the Models folder, select Add, and then select Class. Enter the class name "Movie".

What is class in ASP NET MVC?

The model classes represents domain-specific data and business logic in the MVC application. It represents the shape of the data as public properties and business logic as methods. In the ASP.NET MVC Application, all the Model classes must be created in the Model folder.


2 Answers

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)

like image 109
Shyju Avatar answered Sep 16 '22 14:09

Shyju


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; 
like image 29
Eric J. Avatar answered Sep 18 '22 14:09

Eric J.