拆分查询字符串,并在字母后用其他字符替换某些字符

时间:2019-01-31 09:15:34

标签: c# asp.net

我想构建一个用户友好的URL,其中包含多个变量。 我有这个改写的网址 / IT / crema-per-il-viso-naturale / 30/17 / S125M115

我的问题受到影响的字符串是S125M115

1)我想在S125和M115中拆分此字符串以恢复它并使用数据库中的ID,但我不知道字母后面有多少数字

2)因为我将它们用作过滤器,所以如果我在URL中已经有一个分割的字符串,则需要用另一个字母替换字母后的ID

我使用此重写规则

<rule name="Pagina-Con-Filtri">
      <match url="^([_0-9a-z-]+)/([_0-9a-z-]+)/([0-9]+)/([0-9]+)/([_0-9a-z-]+)$" />
      <action type="Rewrite" url="pagina.aspx?id_gruppo={R:3}&amp;id={R:4}&amp;lang={R:1}&amp;filtri={R:5}" />
    </rule>

建议?

1 个答案:

答案 0 :(得分:0)

我为此解决了

 string[] splittedStr = Regex.Split(Page.ResolveUrl(Request.RawUrl), "(\\D[\\d]+)");
    string newUrl = Page.ResolveUrl(Request.RawUrl);
    newUrl = newUrl.Remove(newUrl.LastIndexOf("/"));
    string urlMarchio = "";
    string urlSottocategoria = "";

    for (int j = 0; j < splittedStr.Length; j++)
    {
        if (splittedStr[j].Contains("S")) urlSottocategoria = splittedStr[j];
        else if (splittedStr[j].Contains("M")) urlMarchio = splittedStr[j];
    }

然后重建网址,我这样做

if (HttpContext.Current.Request.QueryString.Get("filtri") != null && HttpContext.Current.Request.QueryString.Get("filtri") != ""){
            if (urlMarchio.Length > 0 && urlMarchio.Substring(1) != rowFiltroMarchi.GetInt32(rowFiltroMarchi.GetOrdinal("ID_Pagina")).ToString()) linkMarchio.Attributes["href"] = newUrl + "/" + urlSottocategoria + "M" + rowFiltroMarchi.GetInt32(rowFiltroMarchi.GetOrdinal("ID_Pagina"));
            else if (urlMarchio.Length > 0) linkMarchio.Attributes["class"] += " filtro-selezionato";
            else linkMarchio.Attributes["href"] = newUrl + "/" + urlSottocategoria + "M" + rowFiltroMarchi.GetInt32(rowFiltroMarchi.GetOrdinal("ID_Pagina"));
        }
        else linkMarchio.Attributes["href"] = Page.ResolveUrl(Request.RawUrl) + "/M" + rowFiltroMarchi.GetInt32(rowFiltroMarchi.GetOrdinal("ID_Pagina"));