路由简化

发布于 2024-10-04 07:27:28 字数 1290 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2024-10-11 07:27:28

我认为您不能将 things/page2 和 things/from11-14-2010/... 与相同的路线相匹配,因为只有在路线末尾有一个可选值才有意义。但是,您可以将其减少为只有两条路线:

routes.MapRoute(
   "Thing1",
   "Thing/Thing/{from}/{to}/{page}",
   new { controller = "Thing", action = "Thing", page = 1 },
   new { from = @"(\d{1,2}-\d{1,2}-\d{4})?", to = @"(\d{1,2}-\d{1,2}-\d{4})?" }
);

routes.MapRoute(
   "Thing2",
   "Thing/Thing/{page}",
   new { controller = "Thing", action = "Thing", page = 1 },
   new { page = @"(\d*)?" }
);

您可以通过使用正则表达式约束来匹配参数来消除页面、往返前缀。

您还可以通过在路由中设置页面 (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:

routes.MapRoute(
   "Thing1",
   "Thing/Thing/{from}/{to}/{page}",
   new { controller = "Thing", action = "Thing", page = 1 },
   new { from = @"(\d{1,2}-\d{1,2}-\d{4})?", to = @"(\d{1,2}-\d{1,2}-\d{4})?" }
);

routes.MapRoute(
   "Thing2",
   "Thing/Thing/{page}",
   new { controller = "Thing", action = "Thing", page = 1 },
   new { page = @"(\d*)?" }
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文