首页 新闻 会员 周边

.net mvc url重写遇到一个问题,麻烦帮我看下。

0
[已解决问题] 解决于 2017-01-17 17:00
            routes.MapRoute(
               "mylist",
               "{go}_{to}.html",
               new { controller = "Test", action = "List" }
            );

上面是设置的路由规则

访问地址:http://localhost:1841/a_b.html,这样可以访问到

假如a_b.html这个地址少了a或少了b,
http://localhost:1841/a_.html
http://localhost:1841/_b.html

就访问不了了,如果想让其可以访问,怎么处理?

谢谢!

屌丝大叔的笔记的主页 屌丝大叔的笔记 | 初学一级 | 园豆:2
提问于:2017-01-16 18:57
< >
分享
最佳答案
0

一开始真被这个问题难住了,后来换了个思路解决了:

routes.MapRoute(
    name: "mylist",
    template: "{go_to}.html",
    defaults: new { controller = "Test", action = "List"  },
    constraints: new { go_to = @"(go)?_(to)?" });
奖励园豆:5
dudu | 高人七级 |园豆:31007 | 2017-01-16 21:04

果然大神啊,请受我一拜!

龙葛格 | 园豆:782 (小虾三级) | 2017-01-16 23:31
            routes.MapRoute(
                "mylistnew",
                "{go_to}.html",
                new { controller = "Test", action = "List" },
                new { go_to = "([a-zA-Z]*)_([a-zA-Z]*)" }
            );

根据dudu大神的代码改了下,可以使用。

感谢dudu!

屌丝大叔的笔记 | 园豆:2 (初学一级) | 2017-01-17 10:03
其他回答(1)
0

先确定你的规则,再去写,路由只会跟着你的规则走,你了设定你的默认a或者默认b


James.Ying | 园豆:1472 (小虾三级) | 2017-01-16 20:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册