Search code examples
c#asp.net-mvc-5attributerouting

Passing Two Optional parameter to RedirectToRoute


I am passing 2 nullable parameters to a Products action. However I am forced to pass some value in mallId, otherwise I get a no route table matches found error. I want to pass null in mallId and receive in the Products action.

return RedirectToRoute("Products",
                      new
                      {   
                          mallId =(Int32?)null,
                          storeId =(Int32?)storeProducts.StoreId
                      });


[Route("Mall/{mallId?}/Store/{storeId?}/Products", Name = "Products")]
public ActionResult Products(string mallId, long? storeId)
{
    return View(products);
}

Attribute routing is cracking my head but it is great too.


Solution

  • [Route("Mall/{mallId?}/Store/{storeId?}/Products", Name = "Products")]
    public ActionResult Products(string mallId = null, long? storeId)
    {
        return View(products);
    }
    

    and don't pass in a value for mallId