Search code examples
asp.net-mvcattributerouting

Controller method overloading using attribute routing


I can change the method name but can anyone tell me why this doesn't work? How can i make it working without changing method name. Also I don't want to specify the action name in the view. Is it possible?

        [HttpGet]
        [Route("AddUpdateCategories/{storeId}")]
        public ActionResult AddUpdateStoreCategories(int storeId)
        {

            return View();
        }

        [HttpPost]
        public ActionResult AddUpdateStoreCategories(int StoreId,int[] ShopCategoryId)
        {

            return null;
        }

Problem is post action is not getting called on submit.


Solution

  • You don't have to change the method name. The problem is that this post action has no route. If you use attribute routing, you have to specify a route for each action. Both of these actions would end up with the same route attribute, but the [HttpPost] attribute is enough for the routing framework to work out which one to use.

    [Route("AddUpdateCategories/{storeId}")]
    public ActionResult AddUpdateStoreCategories(int storeId)
    {
        return View();
    }
    
    [HttpPost]
    [Route("AddUpdateCategories/{storeId}")]
    public ActionResult AddUpdateStoreCategories(int StoreId,int[] ShopCategoryId)
    {
        return null;
    }