How to replace url-parameter?

2020-03-18 03:44发布

given is an URL like http://localhost:1973/Services.aspx?idProject=10&idService=14.

What is the most straightforward way to replace both url-parameter values(for example 10 to 12 and 14 to 7)?

Regex, String.Replace, Substring or LinQ - i'm a little stuck.

Thank you in advance,

Tim


I ended with following, that's working for me because this page has only these two parameter:

string newUrl = url.Replace(url.Substring(url.IndexOf("Services.aspx?") + "Services.aspx?".Length), string.Format("idProject={0}&idService={1}", Services.IdProject, Services.IdService));

But thank you for your suggestions :)

8条回答
小情绪 Triste *
2楼-- · 2020-03-18 04:30

Here is my implementation:

using System;
using System.Collections.Specialized;
using System.Web; // For this you need to reference System.Web assembly from the GAC

public static class UriExtensions
{
    public static Uri SetQueryVal(this Uri uri, string name, object value)
    {
        NameValueCollection nvc = HttpUtility.ParseQueryString(uri.Query);
        nvc[name] = (value ?? "").ToString();
        return new UriBuilder(uri) {Query = nvc.ToString()}.Uri;
    }
}

and here are some examples:

new Uri("http://host.com/path").SetQueryVal("par", "val")
// http://host.com/path?par=val

new Uri("http://host.com/path?other=val").SetQueryVal("par", "val")
// http://host.com/path?other=val&par=val

new Uri("http://host.com/path?PAR=old").SetQueryVal("par", "new")
// http://host.com/path?PAR=new

new Uri("http://host.com/path").SetQueryVal("par", "/")
// http://host.com/path?par=%2f

new Uri("http://host.com/path")
    .SetQueryVal("p1", "v1")
    .SetQueryVal("p2", "v2")
// http://host.com/path?p1=v1&p2=v2
查看更多
仙女界的扛把子
3楼-- · 2020-03-18 04:32

I found this in an old code example, wouldnt take much to improve it, taking a IEnumerable<KeyValuePair<string,object>> may be better than the current delimeted string.

    public static string AppendQuerystring( string keyvalue)
    {
        return AppendQuerystring(System.Web.HttpContext.Current.Request.RawUrl, keyvalue);
    }
    public static string AppendQuerystring(string url, string keyvalue)
    {
        string dummyHost = "http://www.test.com:80/";
        if (!url.ToLower().StartsWith("http"))
        {
            url = String.Concat(dummyHost, url);
        }
        UriBuilder builder = new UriBuilder(url);
        string query = builder.Query;
        var qs = HttpUtility.ParseQueryString(query);
        string[] pts = keyvalue.Split('&');
        foreach (string p in pts)
        {
            string[] pts2 = p.Split('=');
            qs.Set(pts2[0], pts2[1]);
        }
        StringBuilder sb = new StringBuilder();

        foreach (string key in qs.Keys)
        {
            sb.Append(String.Format("{0}={1}&", key, qs[key]));
        }
        builder.Query = sb.ToString().TrimEnd('&');
        string ret = builder.ToString().Replace(dummyHost,String.Empty);
        return ret;
    }

Usage

   var url = AppendQueryString("http://localhost:1973/Services.aspx?idProject=10&idService=14","idProject=12&idService=17");
查看更多
登录 后发表回答