How to overload the Action Method in asp.net MVC ?


If we have to overload the action Method in asp.net MVC then we can not do it directly like C#. We have to implement some trick like changing the ActionName like this code snippet.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication5.Controllers
{
    public class HomeController : Controller
    {
       

        public ActionResult GetEmpName()
        {
            return Content("This is the test Message");
        }

        [ActionName("GetEmpWithCode")]
        public ActionResult GetEmpName(string EmpCode)
        {
            return Content("This is the test Messagewith Overloaded");
        }

    }
}

Now to run the above controller GetEmpName action method with just give the URL like this

http://localhost:49389/Home/GetEmpName

Overload1

Now to run the above controller GetEmpWithCode action method with just give the URL like this

http://localhost:49389/Home/GetEmpWithCode

Overload2

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.