首页 新闻 会员 周边

mvc路由的一个问题

0
悬赏园豆:5 [待解决问题]
 

比如:

routes.MapRoute(
"Query", // Route name
"Query/p{price}s{size}c{category}", // URL with parameters
new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults );

上面的是错误的,我希望得到如下的

query/p1s2c1  route to query action(price = 1, size = 2, category=1)
query/s2c1 route to query action(price = 0, size = 2, category=1)
query/c1 route to query action(price = 0, size = 0, category=1)

thanks for a help!

mvc
问题补充:

就像好租网 的查询房子的条件进行了Url重写,SEO优化:

haozu是如: /s1_c3_p3

如果不做重写的话就是?s=1&c3&p3

这些条件是不定的,有可能一个都没有,有可能全有

guozili的主页 guozili | 初学一级 | 园豆:47
提问于:2012-04-27 22:40
< >
分享
所有回答(3)
1

 这个路由能否实现没研究过,但要实现起来肯定很复杂,不建议这样操作。按照你的定义,可能要设定很多种可能(排列、组合),这个成本很高。

你不如定义一个通用的action,然后把这个路径值以参数的形式传递给action,然后由action来实现识别。

无之无 | 园豆:5095 (大侠五级) | 2012-04-27 23:24

一定要用路由,价值不是很大的。

不过,如果你一定要坚持,那么:

routes.MapRoute(
    "Query", // Route name
    "Query/p{price}s{size}c{category}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );

routes.MapRoute(
    "Query", // Route name
    "Query/p{price}c{category}s{size}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );

routes.MapRoute(
    "Query", // Route name
    "Query/c{category}p{price}s{size}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/c{category}s{size}p{price}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/s{size}c{category}p{price}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/s{size}p{price}c{category}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/p{price}c{category}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/c{category}p{price}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/s{size}c{category}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/c{category}s{size}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/s{size}p{price}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/p{price}s{size}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/p{price}", // URL with parameters
    new { controller = "Query", action = "Index", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/s{size}", // URL with parameters
    new { controller = "Query", action = "Index", price =0, size = UrlParameter.Optional, category = 0},
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );
routes.MapRoute(
    "Query", // Route name
    "Query/c{category}", // URL with parameters
    new { controller = "Query", action = "Index", price =0, size =0, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );


你自己再把几个默认值的情况补充完整吧。

支持(0) 反对(0) 无之无 | 园豆:5095 (大侠五级) | 2012-04-28 10:31

@笨笨蜗牛: 

谢谢,这样枚举出来太多了,

具体场景是 上面一排过滤条件 有5,6个,还有排序,很多checkbox,都学要重写成如s2_p3_c5_o7_.....

10多个吧,如果mvc支持这样的就好了:

Query/test/(p{price})(s{size})(c{category})   ()表示可有可无
支持(0) 反对(0) guozili | 园豆:47 (初学一级) | 2012-04-28 13:17

@guozili@163.com: 呵呵,愿望是好的,但现实是残酷的。目前MVC还不支持,不等于今后不支持。

其实,MVC的这个解释跟我最开始提的方案是一个策略,只不过是系统化了一点。

如果你一定要这样,那么你可以自己写一个MVC的路由解析模块来实现,你说的这个就变成了现实了。

MVC也好ASP.NET也好,都不过是把一些字符串信息进行模块化、易读化的控制,从而提高开发效率。程序的本身还是离不开0与1的世界,所谓的RAD都是有所牺牲的,只是这个牺牲值得而已。

支持(0) 反对(0) 无之无 | 园豆:5095 (大侠五级) | 2012-04-28 13:40
0

可以实现的,你的路由配置有点问题,少了action

你在QueryController中建一个Test Action,方法如下:

public ActionResult Test(int price = 1, int size = 2, int category = 1)
{
    ViewBag.param = "price:" + price + "    size:" + size + "    category" + category;
    return View();
}

然后路由配置如下:

routes.MapRoute(
    "Query", // Route name
    "Query/test/p{price}s{size}c{category}", // URL with parameters
    new { controller = "Query", action = "Test", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );

注意,这里添加了一个test,然后访问query/test/p1s2c1,就可以得到结果:

price:1 size:2 category1

artwl | 园豆:16736 (专家六级) | 2012-04-28 09:30

假如用户输入的是query/test/s1p2c1呢?按照楼主的这个需求,应该是支持我说的这个情况。

支持(0) 反对(0) 无之无 | 园豆:5095 (大侠五级) | 2012-04-28 09:48

@笨笨蜗牛: 我的方案是支持query/test/s1p2c1 这种写法的,如果地址是 query/s1p2c1 只要在路由中配置一个省略的action就可以了

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2012-04-28 09:52

@artwl: 

笨笨蜗牛

的方式我也考虑过,但是还是想通过mvc自带的路由 机制 来实现。

artwl请看我的问题补充,你的是最简单的,不能满足动态参数的需求。

支持(0) 反对(0) guozili | 园豆:47 (初学一级) | 2012-04-28 10:16
0

在路由中你要有缺省 值,才會正確的。

無限遐想 | 园豆:3740 (老鸟四级) | 2012-04-28 11:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册