【问题标题】:Passing Two Optional parameter to RedirectToRoute将两个可选参数传递给 RedirectToRoute
【发布时间】:2015-04-22 19:50:04
【问题描述】:

我将 2 个可为空的参数传递给 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);
}

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

【问题讨论】:

    标签: c# asp.net-mvc-5 attributerouting


    【解决方案1】:
    [Route("Mall/{mallId?}/Store/{storeId?}/Products", Name = "Products")]
    public ActionResult Products(string mallId = null, long? storeId)
    {
        return View(products);
    }
    

    并且不要为 mallId 传递值

    【讨论】:

      【解决方案2】:

      您应该为 mallId 提供一个默认值。您分配参数的方式使得不可能在路由组合中不提供 mallId:

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

      【讨论】:

      • 两个答案都有效。赞成。谢谢。接受第一个
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 2014-09-12
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多