【发布时间】:2014-12-07 21:18:14
【问题描述】:
我有两个必需的路径:
1: localhost:1207/Blog/2014/12/Article-Name
2:本地主机:1207/Blog/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在这两条路线中真的是可选的吗? -
年月参数不应该是可选的
-
如果我的回答对你有用。
标签: asp.net-mvc-5 asp.net-mvc-routing url-routing