Can you overload controller methods in ASP.NET MVC?

前端 未结 17 1663
无人共我
无人共我 2020-11-22 08:10

I\'m 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 arguments. Is this some

相关标签:
17条回答
  • 2020-11-22 08:44

    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.

    0 讨论(0)
  • 2020-11-22 08:44

    You can use [ActionName("NewActionName")] to use the same method with a different name:

    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");
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:44

    I needed an overload for:

    public ActionResult Index(string i);
    public ActionResult Index(int groupId, int itemId);
    

    There were few enough arguments where I ended up doing this:

    public ActionResult Index(string i, int? groupId, int? itemId)
    {
        if (!string.IsNullOrWhitespace(i))
        {
            // parse i for the id
        }
        else if (groupId.HasValue && itemId.HasValue)
        {
            // use groupId and itemId for the id
        }
    }
    

    It's not a perfect solution, especially if you have a lot of arguments, but it works well for me.

    0 讨论(0)
  • 2020-11-22 08:45

    There is only one public signature allowed for each controller method. If you try to overload it, it will compile, but you're getting the run-time error you've experienced.

    If you're not willing to use different verbs (like the [HttpGet] and [HttpPost] attributes) to differentiate overloaded methods (which will work), or change the routing, then what remains is that you can either provide another method with a different name, or you can dispatch inside of the existing method. Here's how I did it:

    I once came into a situation where I had to maintain backwards compatibility. The original method expected two parameters, but the new one had only one. Overloading the way I expected did not work because MVC didn't find the entry point any more.

    To solve that, I did the following:

    1. Changed the 2 overloaded action methods from public to private
    2. Created one new public method which contained "just" 2 string parameters. That one acted as a dispatcher, i.e.:

      public ActionResult DoSomething(string param1, string param2)
      {
          if (string.IsNullOrEmpty(param2))
          {
              return DoSomething(ProductName: param1);
          }
          else
          {
              int oldId = int.Parse(param1);
              return DoSomething(OldParam: param1, OldId: oldId);
          }
      }
      
      
      private ActionResult DoSomething(string OldParam, int OldId)
      {
          // some code here
          return Json(result);
      }
      
      
      private ActionResult DoSomething(string ProductName)
      {
          // some code here
          return Json(result);
      }
      

    Of course, this is a hack and should be refactored later. But for the time being, it worked for me.

    You can also create a dispatcher like:

    public ActionResult DoSomething(string action, string param1, string param2)
    {
        switch (action)
        {
            case "update":
                return UpdateAction(param1, param2);
            case "remove":
                return DeleteAction(param1);
        }
    }
    

    You can see, that UpdateAction needs 2 parameters, while DeleteAction just needs one.

    0 讨论(0)
  • 2020-11-22 08:46

    As far as I know you can only have the same method when using different http methods.

    i.e.

    [AcceptVerbs("GET")]
    public ActionResult MyAction()
    {
    
    }
    
    [AcceptVerbs("POST")]
    public ActionResult MyAction(FormResult fm)
    {
    
    }
    
    0 讨论(0)
  • 2020-11-22 08:46

    Create the base method as virtual

    public virtual ActionResult Index()
    

    Create the overridden method as override

    public override ActionResult Index()
    

    Edit: This obviously applies only if the override method is in a derived class which appears not to have been the OP's intention.

    0 讨论(0)
提交回复
热议问题