Click here to Skip to main content
13,863,642 members
Click here to Skip to main content
Add your own
alternative version

Stats

50.5K views
10 bookmarked
Posted 25 Nov 2014
Licenced CPOL

Can we overload MVC controller action methods? (MVC Polymorphism)

, 4 Dec 2014
Rate this:
Please Sign up or sign in to vote.
In this article we will learn can we overload mvc controller action methods.

This Saturday and Sunday when i was taking MVC class in Mumbai one of the participants asked this weird question, Can we overload MVC action methods?. This question was asked to him during a MVC interview recently.

For moment I wondered why interviewer such kind of questions which does not test anything about programmer's ability. But let's leave that argument from some other day and lets answer this question.

Well the answer is YES and NO depending on your expectation. Now before you turn me in to a politician who answers from both side's let me explain you why i have given such kind of a dubious answer.

Overloading or in other words polymorphism is a feature of object oriented programming. So if you have some kind of a below controller code which has methods overloaded with same name and different argument's it would compile very well.

<pre lang=”html”>public class CustomerController : Controller { // // GET: /Customer/ publicActionResultLoadCustomer() { return Content("LoadCustomer"); } publicActionResultLoadCustomer(string str) { return Content("LoadCustomer with a string"); } }

But now if you are thinking that when you call "http://localhost:3450/Customer/LoadCustomer/" it should invoke "LoadCustomer" and when you call "http://localhost:3450/Customer/LoadCustomer/test" it should invoke "LoadCustomer(string str)" you are WRONG.

If you try to this you will end with a below error. Read the word "Ambiguous" in the error , does something click. Ok , let me explain in more detail what the below error means.


Polymorphism is a part of C# programming while HTTP is a protocol. HTTP does not understand polymorphism it works on the concept's or URL and URL can only have unique name's. So HTTP does not implement polymorphism.

And i know if you answer with the above argument MVC interviewer would still press that he wants to implement polymorphism , so how do we go about doing it.

If you wish to keep polymorphism and also want the HTTP request to work you can decorate one of the methods with "ActionName" as shown in the below code.

<pre lang=”html”>public class CustomerController : Controller { // // GET: /Customer/ publicActionResultLoadCustomer() { return Content("LoadCustomer"); } [ActionName("LoadCustomerbyName")] publicActionResultLoadCustomer(string str) { return Content("LoadCustomer with a string"); } }

So now you can invoke with URL structure "Customer/LoadCustomer" the "LoadCustomer" action and with URL structure "Customer/LoadCustomerByName" the "LoadCustomer(string str)" will be invoked.





















In case you want to start learning MVC , below is a nice MVC step by step video series which will help you to learn MVC in 16 hours i.e. 2 day's. Give a try.


License

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

Share

About the Author


You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMessage Closed Pin
31-May-18 5:16
memberMember 1383712331-May-18 5:16 
Bug[My vote of 1] Hope this is not the perfect example for MVC Polymorphism Pin
armugam.indrani(Hyd)23-May-16 4:19
memberarmugam.indrani(Hyd)23-May-16 4:19 
QuestionIt still needs a different view Pin
Member 1253495619-May-16 22:09
memberMember 1253495619-May-16 22:09 
Questiondepends on use case... Pin
ely_bob9-Dec-14 7:58
professionalely_bob9-Dec-14 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 5 Dec 2014
Article Copyright 2014 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid