Click here to Skip to main content
15,885,278 members
Articles / Polymorphism

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (10 votes)
4 Dec 2014CPOL2 min read 63.7K   10   4
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.

”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.

Image 1

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.

”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.

Image 2

 

 

 

 

 





 
Image 3











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.

Image 4

For further reading do watch the below interview preparation videos and step by step video series.

License

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


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
Bug[My vote of 1] Hope this is not the perfect example for MVC Polymorphism Pin
armugam.indrani(Hyd)23-May-16 3:19
armugam.indrani(Hyd)23-May-16 3:19 
QuestionIt still needs a different view Pin
Member 1253495619-May-16 21:09
Member 1253495619-May-16 21:09 
Questiondepends on use case... Pin
ely_bob9-Dec-14 6:58
professionalely_bob9-Dec-14 6:58 
Although you are correct that you cannot call those endpoints without getting an "ambiguous error"... You're. Approach doesn't actually solve the problem.. The correct approach for this I to set the query param(s) to default.. To say null... This will cause a single endpoint to "behave" as though it is polymorphic..
"The conversations he was having with himself were becoming ominous."-.. On the radio...
If you want answers: provide lots of information, including tiny details!

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.