Skip to main content
 首页 » 编程设计

c#之将两个可选参数传递给 RedirectToRoute

2024年02月06日15powertoolsteam

我将 2 个可为 null 的参数传递给 Products 操作。但是,我不得不在 mallId 中传递一些值,否则我会收到 no route table matches found 错误。我想在 mallId 中传递 null 并在 Products 操作中接收。

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

属性路由让我头疼,但它也很棒。

请您参考如下方法:

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

并且不要传入 mallId 的值