Can you overload controller methods in ASP.NET MVC?

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

    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");
        }
    }
    

提交回复
热议问题