Home > DeveloperSection > Forums > How to overload controller in Asp.Net MVC?
Ashish Pandey

Total Post:128

Points:898
Posted on    October-07-2014 11:11 PM

 ASP.NET MVC ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 725  View(s)
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?


Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-07-2014 11:14 PM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick