将两个Optional参数传递给RedirectToRoute

时间:2015-04-22 19:50:05

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

我将2个可空参数传递给Products操作。但是我被迫在mallId中传递一些值,否则我会收到no route table matches found错误。我想在null中传递mallId并在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);
}

属性路由正在破解我的头脑,但它也很棒。

2 个答案:

答案 0 :(得分:2)

# First, vectorize function
rottenrate <- function(movie){
  require(RJSONIO)
  link <- paste("http://www.omdbapi.com/?t=", movie, "&y=&plot=short&r=json&tomatoes=true", sep = "")
  jsonData <- fromJSON(link)
  return(jsonData)
}
vrottenrate <- Vectorize(rottenrate, "movie", SIMPLIFY = FALSE)

# Now, query and combine
movies <- c("inception", "toy story")
df <- do.call(rbind, lapply(vrottenrate(movies), function(x) as.data.frame(t(x), stringsAsFactors = FALSE)))
dplyr::glimpse(df)
# Observations: 2
# Variables:
# $ Title             (chr) "Inception", "Toy Story"
# $ Year              (chr) "2010", "1995"
# $ Rated             (chr) "PG-13", "G"
# $ Released          (chr) "16 Jul 2010", "22 Nov 1995"
# $ Runtime           (chr) "148 min", "81 min"
# $ Genre             (chr) "Action, Mystery, Sci-Fi", "Animation, 
# ...

并且不传递mallId的值

答案 1 :(得分: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);
}