Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,

I have been working on Asp.Net MVC from past 3-4 months and got a question in my mind, which is really puzzling me out.

I have searched the same in the internet but came up with nothing.

Problem:
We all know that MVC will work as per the URL -> first it will look for the controller and then the specified action and parameter(s).
But, any user-defined controller is a class and it inherits from the base class "Controller".
Now, if I declare a parameterized constructor in my controller. How to invoke it?

Example:

What I have tried:

public MyController : Controller
{
public int index;

public MyController(int i)
{
index = 10;
}

// Action
public ActionResult someAction()
{
// implementation
}
}

Now, how to call the parameterized constructor of the controller?
Posted
Updated 21-Nov-17 23:38pm

I am not sure whether I am following you or not. But why do you even want to have a controller with that defined parameter?

It is not the controller that follows the URL, but instead the actions that do so. Your controller — if really needed — can hold member fields to hold those values, and they can be accessed in the action.

Also, you have the freedom to accept the placeholder variables in routing. I would do the same coding in the different manner, this seems somewhat easier and straightforward.
C#
public MyController : Controller
{
   public int index;

   public MyController()
   {
   }

   // Action
   [Route("controller/{i}")]
   public ActionResult someAction(int i)
   {
      // implementation
   }
}

Lastly, ASP.NET does support parameters, to understand how they work you can learn more about Dependency Injection to learn how framework would inject values for the parameters of a constructor. ASP.NET MVC 4 Dependency Injection | Microsoft Docs[^]
.net - How do constructor parameters of a MVC Controller get set? - Software Engineering Stack Exchange[^]
 
Share this answer
 
Comments
Member 11072126 22-Nov-17 5:19am    
Thanks for your response Afzaal. There is no such need for me to pass the parameter in the constructor. I was just thinking about this because I thought that it is also a class and if I can pass any parameter through the constructor or not?
If I can pass then how it will be invoked unless I pass any value into the constructor.
But, we don't create any object of the controller explicitly, the controller and actions are called on the basis of my routing path.
So, is it possible to create any parameterized constructor in controller? if yes, how to invoke it and if No, then why?
MVC uses a controller factory to create your controllers. Obviously it doesn't know what value to pass your constructor or where to get it from, so it only allows controllers to be created if they have parameterless constructors. To allow for controllers with params you'd have to write your own factory and create the constructor yourself, reading the value from where it needs to be read from and passing it yourself.

Understanding and Extending Controller Factory in MVC[^]

Chances are you don't want\need to do that however, there is probably some other solution but unless we know what the value is for and where it comes from it's hard to say.
 
Share this answer
 
Comments
Member 11072126 23-Nov-17 1:38am    
Ok. Thanks. So, MVC internally follows Factory Design Pattern? Can you suggest any article if I would like to know hoe MVC works internally? I mean, when the request is coming, which interface or class it is calling internally and all these concepts

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900