Can you overload controller methods in ASP.NET MVC?

前端 未结 17 1669
无人共我
无人共我 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:32

    You could use a single ActionResult to deal with both Post and Get:

    public ActionResult Example() {
       if (Request.HttpMethod.ToUpperInvariant() == "GET") {
        // GET
       }
       else if (Request.HttpMethod.ToUpperInvariant() == "POST") {
         // Post  
       }
    }
    

    Useful if your Get and Post methods have matching signatures.

提交回复
热议问题