Coder Perfect

In ASP.NET MVC, can you overload controller methods?

Problem

In ASP.NET MVC, I’m curious if you may overload controller methods. When I try, I receive the following error. Different arguments are accepted by the two approaches. Is there any way to make this happen?

Asked by Papa Burgundy

Solution #1

If you want your code to do overloading, you can use the attribute.

[ActionName("MyOverloadedName")]

However, for the same http method, you’ll need to use a different action name (as others have said). At that point, it’s just semantics. Would you prefer have the name or the attribute in your code?

http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx Phil has an essay on this: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

Answered by JD Conley

Solution #2

Yes. I was able to accomplish this by setting the HttpGet/HttpPost (or comparable AcceptVerbs attribute) for each controller function to something unique, such as HttpGet or HttpPost but not both. That way, it can determine which method to employ based on the type of request.

[HttpGet]
public ActionResult Show()
{
   ...
}

[HttpPost]
public ActionResult Show( string userName )
{
   ...
}

To prevent duplicating code, one proposal I have for a situation like this is to create a private implementation that both of your public Action methods rely on.

Answered by tvanfosson

Solution #3

Another thing you could do is create a method that can both have and not have a parameter.

Why not give it a shot…

public ActionResult Show( string username = null )
{
   ...
}

This has worked for me… and you can actually test if you have the incoming parameter in this one procedure.

Answered by Farrel

Solution #4

No, No, and No are the answers. Try the controller code below, where the “LoadCustomer” variable is overloaded.

public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult LoadCustomer()
        {
            return Content("LoadCustomer");
        }
        public ActionResult LoadCustomer(string str)
        {
            return Content("LoadCustomer with a string");
        }
    }

If you try to invoke the “LoadCustomer” action you will get error as shown in the below figure.

C# programming has polymorphism, but HTTP is a protocol. HTTP isn’t aware of polymorphism. HTTP is based on the concept of URLs, which can only have one unique name. As a result, HTTP does not support polymorphism.

To correct this, we must utilize the “ActionName” attribute.

public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult LoadCustomer()
        {
            return Content("LoadCustomer");
        }

        [ActionName("LoadCustomerbyName")]
        public ActionResult LoadCustomer(string str)
        {
            return Content("LoadCustomer with a string");
        }
    }

So, if you go to URL “Customer/LoadCustomer,” the “LoadCustomer” action will be called, and if you go to URL “Customer/LoadCustomerByName,” the “LoadCustomer(string str)” action will be called.

I got the above solution from this codeproject article —> Overloading MVC Actions

Answered by Shivprasad Koirala

Solution #5

To solve this problem, create an ActionMethodSelectorAttribute that examines the MethodInfo for each action and compares it to the submitted Form values, then rejects any method that does not match the posted Form values (excluding the button name, of course).

Here’s an illustration: – http://blog.abodit.com/2010/02/asp-net-mvc-ambiguous-match/ – http://blog.abodit.com/2010/02/asp-net-mvc-ambiguous-match/

This, however, is not a good idea.

Answered by Ian Mercer

Post is based on https://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc