Can we overload Actions / Controllers in MVC? (MVC Interview questions)
- By Shiv Prasad Koirala in .Net
- Nov 25th, 2014
- 11298
- 0
If you are preparing for MVC interview download our 100 MVC Interview question Ebook.
Let us first try to understand what the MVC interviewer is trying to ask. If you have a controller as shown in the below code snippet and in the controller if you have overloaded methods will it work. For example in the below "Customer" controller we have two "LoadCustomer" methods one with a parameter and one without a parameter.
So will this work. The answer is No, No , No , No and No.
If you try to invoke "LoadCustomer" you willland up with the below error.
In order to resolve the same you decorate one of your actions with "ActionName" attribute as shown in the below code.
So now if you make a call to URL "Customer/LoadCustomer" the "LoadCustomer" action will be invoked and with URL structure "Customer/LoadCustomerByName" the "LoadCustomer(string str)" will be invoked.
Learn MVC in 2 days (16 hours) start from the below video
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more