asp.net mvc 根据路由生成正确的url

假设存在这样一段路由配置:

routes.MapRoute(
name: "ProductList1_01",
url: "pl/{bigSortId}_{smallSortId}_{brandId}.html",
defaults: new { controller = "NewStore", action = "ProductList" },
namespaces: mallNS
);
routes.MapRoute(
name: "ProductList1_02",
url: "pl/{bigSortId}_{smallSortId}.html",
defaults: new { controller = "NewStore", action = "ProductList" },
namespaces: mallNS
);
routes.MapRoute(
name: "ProductList1_03",
url: "pl/{bigSortId}.html",
defaults: new { controller = "NewStore", action = "ProductList" },
namespaces: mallNS
);

 

如果我希望自动匹配到 pl/{bigSortId}_{smallSortId}.html,我会这样写:

@Url.Action("ProductList", new { bigSortId = Model.BigSortId, smallSortId = Model.SmallSortId, brandId = "" })

但如果页面存在form,并且存在向当前页面的post brandId表单元素,可能造成生成这样的Url:

/pl/11_02.html?brandId=293,导致虽然路由匹配正确,但仍然出现了问号及后面的参数(而这里我是希望生成的链接为:/pl/11_02.html)

 

为了达到这个目的,而又不将Url写死,修改路由配置为:

routes.MapRoute(
name: "ProductList1_01",
url: "pl/{bigSortId}_{smallSortId}_{brandId}.html",
defaults: new { controller = "NewStore", action = "ProductList" },
namespaces: mallNS
);
routes.MapRoute(
name: "ProductList1_02",
url: "pl/{bigSortId}_{smallSortId}.html",
defaults: new { controller = "NewStore", action = "ProductList" ,brandId="" },
namespaces: mallNS
);
routes.MapRoute(
name: "ProductList1_03",
url: "pl/{bigSortId}.html",
defaults: new { controller = "NewStore", action = "ProductList", smallSortId = "", brandId="" },
namespaces: mallNS
);

 

解决此问题。

转载于:https://www.cnblogs.com/gesenkof99/p/3569828.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值