如何将 t4mvc 路由助手与自定义路由约束一起使用

发布于 2024-11-29 16:46:36 字数 1325 浏览 0 评论 0原文

我在当前项目中使用 t4mvc 并尝试使用包含的路由助手,但是当我尝试使用如下所示的自定义约束时,

     routes.MapRoute(
        "def_filtered_reports_route",
        "reports/{samplePoint}/{fromDate}/{toDate}",
        MVC.Report.Results(null, null, null),
        new
        {
            samplePoint = new SamplePointExistsConstraint(),
            fromDate = new DateTimeConstraint(),
            toDate = new DateTimeConstraint()
        }
        );

它会抛出一个 ArgumentException ,说明 具有相同键的项目已经添加。

如果我这样写

 routes.MapRoute(
    "def_filtered_reports_route",
    "reports/{samplePoint}/{fromDate}/{toDate}",
    MVC.Report.Results(null, null, null) );

或这样

    routes.MapRoute(
           "def_filtered_reports_route",
           "reports/{samplePoint}/{fromDate}/{toDate}",
           new
           {
               controller = "Report",
               action = "Results",
               fromDate = "",
               toDate = "",
               samplePoint = ""
           },
           new
           {
               fromDate = new DateTimeConstraint(),
               toDate = new DateTimeConstraint(),
               samplePoint = new SamplePointExistsConstraint()
           });

写它就可以正常工作。

有什么我遗漏的或者 t4mvc 不支持自定义约束吗

im using t4mvc in my current project and am trying to use the routing helper included however when i try to use custom constraints as below

     routes.MapRoute(
        "def_filtered_reports_route",
        "reports/{samplePoint}/{fromDate}/{toDate}",
        MVC.Report.Results(null, null, null),
        new
        {
            samplePoint = new SamplePointExistsConstraint(),
            fromDate = new DateTimeConstraint(),
            toDate = new DateTimeConstraint()
        }
        );

it throws an ArgumentException stating An item with the same key has already been added.

if i write it like this

 routes.MapRoute(
    "def_filtered_reports_route",
    "reports/{samplePoint}/{fromDate}/{toDate}",
    MVC.Report.Results(null, null, null) );

or like this

    routes.MapRoute(
           "def_filtered_reports_route",
           "reports/{samplePoint}/{fromDate}/{toDate}",
           new
           {
               controller = "Report",
               action = "Results",
               fromDate = "",
               toDate = "",
               samplePoint = ""
           },
           new
           {
               fromDate = new DateTimeConstraint(),
               toDate = new DateTimeConstraint(),
               samplePoint = new SamplePointExistsConstraint()
           });

it works fine.

Is there something I'm missing or does t4mvc not support custom constraints

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

怪我鬧 2024-12-06 16:46:36

尝试在约束之前为默认值传递一个额外的空值。例如

        routes.MapRoute(
           "def_filtered_reports_route",
           "reports/{samplePoint}/{fromDate}/{toDate}",
           MVC.Report.Results(null, null, null),
           null /*defaults*/,
           new {
               samplePoint = new SamplePointExistsConstraint(),
               fromDate = new DateTimeConstraint(),
               toDate = new DateTimeConstraint()
           }
           );

Try passing an extra null for the defaults before the constraints. e.g.

        routes.MapRoute(
           "def_filtered_reports_route",
           "reports/{samplePoint}/{fromDate}/{toDate}",
           MVC.Report.Results(null, null, null),
           null /*defaults*/,
           new {
               samplePoint = new SamplePointExistsConstraint(),
               fromDate = new DateTimeConstraint(),
               toDate = new DateTimeConstraint()
           }
           );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文