Thursday 15 December 2016

How to achieve method overloading in MVC? Or is it possible to achieve Method overloading in asp.net mvc?


Method overloading is possible in asp.net MVC.

This is general question which is asked in an interview.
Overloading or in other word we can say Polymorphism, it is the part of c# language. Http does not aware of this thing. Http works on URL concept and URL should be unique.

We can achieve overloading by the use of acceptverb attribute like HttpGet or HttpPost etc.
We can also use ActionMethod attribute above the method name in controller.

Example:-

public class HomeController : Controller
    {

        public ActionResult MethodName()
        {
            return Content("Method overloading with no parameter.");
        }

        [ActionName("TestMethod")]
        public ActionResult MethodName(string name)
        {
            return Content("Method overloading with with  parameter:" + name);
        }
    }

Note:-
If you didn’t put attribute ActionName above Method name then it will error at run time.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.