开发者

custom constraint in mvc3 application

开发者 https://www.devze.com 2023-02-24 15:10 出处:网络
I am creating a custom route in asp.net MVC3 application and I want to create a route constraint whi开发者_如何学Pythonch will check if the url has integer as second part of the parameter. If so, I ne

I am creating a custom route in asp.net MVC3 application and I want to create a route constraint whi开发者_如何学Pythonch will check if the url has integer as second part of the parameter. If so, I need to redirect to different action.

product/12

product/12-2-1990


Here's how I do it.

    ''# MapRoute allows for a dynamic product ID
    routes.MapRoute("Products", "product/{id}",
                             New With {.controller = "product",
                                       .action = "index"},
                             New With {.id = "[0-9]+") ''# this forces the ID to only be a number.

Then you will create your redirect logic separately. I would recommend using an ActionFilter to do your redirect work. You write it once and it can be implemented all over.

0

精彩评论

暂无评论...
验证码 换一张
取 消