zoukankan      html  css  js  c++  java
  • 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
    );

    解决此问题。

  • 相关阅读:
    Linux如何同步时间/更新时间
    Linux关机/重启/及注销命令
    Linux如何查看别名和取消别名
    Linux查看历史记录
    Linux命令行快捷键有哪些
    win10产品密钥 win10永久激活密钥(可激活win10所有版本 )
    Xshell如何连接
    Xshell6 优化
    逻辑运算符
    可变类型与不可变类型
  • 原文地址:https://www.cnblogs.com/gesenkof99/p/3569828.html
Copyright © 2011-2022 走看看