路由简化
我正在使用 ASP.NET MVC3 RC1 开发一个项目,想知道是否可以简化我的路线。
我有这个控制器操作:
public ActionResult Things(DateTime? fromDate, DateTime? toDate, int? pageNo)
{
DataLayer dataLayer = new DataLayer();
int page = pageNo.HasValue ? pageNo.Value : 1;
List<Thing> things = dataLayer.GetThingsByDateRangePaged(User.Identity.Name, fromDate, toDate, pageNo, PageSize);
return View(things);
}
我希望能够通过以下网址访问此控制器操作:
- things
- things/page2
- things/from11-14-2010/to11-24-2010
- things/from11-14-2010/ to11-24-2010/page2
我目前已经制定了这些路线:
routes.MapRoute("Things",
"Things",
new { controller = "Thing", action = "Things" }
);
routes.MapRoute("ThingsPaged",
"Things/Page{pageNo}",
new { controller = "Thing", action = "Things" }
);
routes.MapRoute("ThingsCertainPeriod",
"Things/From{fromDate}/To{toDate}",
new { controller = "Thing", action = "Things" }
);
routes.MapRoute("ThingsCertainPeriodPaged",
"Things/From{fromDate}/To{toDate}/Page{pageNo}",
new { controller = "Thing", action = "Things" }
);
在我看来,我的路线已经成熟,可以进行重构,但我不完全确定如何消除冗余。 UrlParameter.Optional 似乎并不像我尝试时想象的那样工作。我的路线可以简化为一条路线吗?
I'm working on a project using ASP.NET MVC3 RC1 and was wondering if my routes can be simplified.
I've got this controller action:
public ActionResult Things(DateTime? fromDate, DateTime? toDate, int? pageNo)
{
DataLayer dataLayer = new DataLayer();
int page = pageNo.HasValue ? pageNo.Value : 1;
List<Thing> things = dataLayer.GetThingsByDateRangePaged(User.Identity.Name, fromDate, toDate, pageNo, PageSize);
return View(things);
}
I'd like to be able to access this controller action by the following urls:
- things
- things/page2
- things/from11-14-2010/to11-24-2010
- things/from11-14-2010/to11-24-2010/page2
I've currently have these routes mapped out:
routes.MapRoute("Things",
"Things",
new { controller = "Thing", action = "Things" }
);
routes.MapRoute("ThingsPaged",
"Things/Page{pageNo}",
new { controller = "Thing", action = "Things" }
);
routes.MapRoute("ThingsCertainPeriod",
"Things/From{fromDate}/To{toDate}",
new { controller = "Thing", action = "Things" }
);
routes.MapRoute("ThingsCertainPeriodPaged",
"Things/From{fromDate}/To{toDate}/Page{pageNo}",
new { controller = "Thing", action = "Things" }
);
Seems to me that my routes are ripe for refactoring but I'm not entirely sure how to get rid of the redundancy. UrlParameter.Optional doesn't seem to work like how I thought it did when I experimented with it. Can my routes be simplified to one route?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不能将 things/page2 和 things/from11-14-2010/... 与相同的路线相匹配,因为只有在路线末尾有一个可选值才有意义。但是,您可以将其减少为只有两条路线:
您可以通过使用正则表达式约束来匹配参数来消除页面、往返前缀。
您还可以通过在路由中设置页面 (1) 的默认值来简化控制器,这样它就不必为空。否则,您将必须使用 UrlParameter.Optional 作为第一个路由中的页面值。
I don't think you can match things/page2 and things/from11-14-2010/... with the same route because it only makes sense to have an optional value at the end of a route. However you can cut it down to just two routes:
You can eliminate the page, to and from prefixes by using regex constraints to match the parameters.
You can also simplify the controller by setting the default value for page (1) in the route, so that it need not be a nullable. Otherwise you would have to use UrlParameter.Optional for the page value in the first route.