HOW TO OVERLOAD CONTROLLER IN ASP.NET MVC?

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
 1061  View(s)
Ratings:
Rate this:
Im curious to see if you can overload controller methods in ASP.Net MVC. Whenever I try, I get the error below. The two methods accept different arguements. Is this something that cannot be done?
  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: How to overload controller in Asp.Net MVC?

    Yes. I've been able to do this by setting the HttpGet/HttpPost (or equivalent AcceptVerbs attribute) for each controller method to something distinct, i.e., HttpGet or HttpPost, but not both. That way it can tell based on the type of request which method to use.

    [HttpGet]

            public ActionResult Show()

            {

               ...

            }

     

            [HttpPost]

            public ActionResult Show(string userName)

            {

               ...

            }

    One suggestion I have is that, for a case like this, would be to have a private implementation that both of your public Action methods rely on to avoid duplicating code.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!