我有兩個必需的路徑:正確映射正確的路線行動,類似的URL
1:本地主機:1207 /博客/ 2014/12 /條,名稱
2:本地主機:1207 /博客/ 2014/12
爲每個路由現在我已經寫了自定義映射
1:
routes.MapRoute(
name: "BlogArticle",
url: "blog/{year}/{month}/{title}",
defaults: new
{
controller = "Blog",
action = "Detail",
year = UrlParameter.Optional,
month = UrlParameter.Optional,
title = UrlParameter.Optional
}
);
2:
routes.MapRoute(
name: "BlogMonthList",
url: "blog/{year}/{month}",
defaults: new
{
controller = "Blog",
action = "MonthList",
year = UrlParameter.Optional,
month = UrlParameter.Optional
});
第二個不起作用,我不確定爲什麼。 作爲答案的一部分,你能解釋一下爲什麼?
我的解決方案必須使用RouteConfig.cs我控制器
例子:
public BlogController : Controller{
public ActionResult MonthList(int year, int month)
{
var model = new MonthArticlesModel()
{
Year = year,
Month = month
};
return View(model);
}
public ActionResult Detail(int year, int month, string title)
{
var model = new DetailModel();
return View(model);
}
}
這兩條路線中的'year'和'month'是否都是可選的? – 2014-12-07 23:41:01
年份的月份參數不應該是可選的 – cpoDesign 2014-12-08 07:07:17
這種情況下,我的答案應該爲你工作。 – 2014-12-08 07:10:46