Polymorphism-Funtion Overloading in ASP.NET MVC



In this article we will learn how to implement polymorphism in ASP.NET MVC.
Here we will focus on function overloading concept of polymorphism.
As we all know the concept of function overloading in c#. In short function overloading enables developer to create function with same name and different number of parameter. For example:
Here you can see I have created two functions with same name but number of parameter is different.
       public void GetEmployee()
        {
            //write some code
        }
        public void GetEmployee(string employee_Id)
        {
            //write some code
        } Now I am going to show you how you can do same in ASP.NET MVC. In MVC you cannot implement function overloading simply as which I have shown in above example. We will see you what happen when we do function overloading in ASP.NET MVC like above example. Let’s see with example. In below example you can see I have created a controller class and two methods with same name but number of parameter is different.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DemoMVC.Controllers
{
    public class DefaultController : Controller
    {
        public ActionResult GetEmployee()
        {
            return Content("This is : GetEmployee method without parameter");
        }
        public ActionResult GetEmployee(string employee_Id)
        {
            return Content("This is : GetEmployee method with a parameter");
        }



    }}
Now I am going to run application. After hitting run application we need put this
http://localhost /Default/ GetEmployee path in URL to call Default controller and method Getcustomer. Now we can see below error in browser.
This error says there is ambiguity between action method names.  So that means we cannot implement function overloading simply in ASP.NET MVC. And now I have one question in mind why function overloading not implemented in ASP.NET MVC as we implement same in asp.net and in simple class files.
Here is my answer also: In ASP.NET MVC we call action/Method name of any controller class through URL like: http://localhost /Default/ GetEmployee and as per HTTP URL rule URL should be unique. So this is a reason why function overloading not working in ASP.NET MVC. But we can do this by adding some code above action/method in ASP.NET MVC controller class. I will show you how we can do.
Now we need to add ActionName attribute with different value above the any Action method name which you have used for function overloading.
[ActionName("GetEmployeewithParameter")]



After that I am going to press F5/Run this application and you can see output of this method


Here is output of second method:

Ok .Now time to understand the role of ActionName attribute. You can understand ActionName role  using below diagram. Basically ActionName work as a Mapper. Its map action method to URL with different name like GetEmployeewithParameter
Thanks for reading my article.
You can also read article related to ASP.NET MVC.
Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

3 comments:

  1. public ActionResult GetEmployee()
    {
    return Content("This is : GetEmployee method without parameter");
    }

    [ActionName("GetEmployeewithParameter")]
    public ActionResult GetEmployee(string employee_Id)
    {
    return employee_Id;
    }
    But this is not working, returns employee_Id as null;

    ReplyDelete
    Replies
    1. You need to configure your App_Start/RouteConfig.cs file:
      routes.MapRoute(
      "Demo",
      "Demo/{action}/{employee_Id}",
      new { controller = "Demo", action = "GetEmployeewithParameter", employee_Id = "" }
      );
      routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );

      In controller class you need to write this:
      [ActionName("GetEmployeewithParameter")]
      public ActionResult GetEmployee(string employee_Id)
      {
      return Content(employee_Id);
      }

      Delete
    2. After that call this controller action method using below URL
      http://localhost/Demo/GetEmployeewithParameter/12

      Delete