如何替换url-parameter?

Tim*_*ter 7 .net c# asp.net string url

给出的是像这样的URL http://localhost:1973/Services.aspx?idProject=10&idService=14.

替换两个url参数值的最直接的方法是什么(例如10到12和14到7)?

正则表达式,String.Replace,子串或LinQ - 我有点卡住了.

先感谢您,

蒂姆


我结束了跟随,这对我有用,因为这个页面只有这两个参数:

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

但是谢谢你的建议:)

Ste*_*uer 14

这就是我要做的:

public static class UrlExtensions
{
    public static string SetUrlParameter(this string url, string paramName, string value)
    {
        return new Uri(url).SetParameter(paramName, value).ToString();
    }

    public static Uri SetParameter(this Uri url, string paramName, string value)
    {           
        var queryParts = HttpUtility.ParseQueryString(url.Query);
        queryParts[paramName] = value;
        return new Uri(url.AbsoluteUriExcludingQuery() + '?' + queryParts.ToString());
    }

    public static string AbsoluteUriExcludingQuery(this Uri url)
    {
        return url.AbsoluteUri.Split('?').FirstOrDefault() ?? String.Empty;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

string oldUrl = "http://localhost:1973/Services.aspx?idProject=10&idService=14";
string newUrl = oldUrl.SetUrlParameter("idProject", "12").SetUrlParameter("idService", "7");
Run Code Online (Sandbox Code Playgroud)

要么:

Uri oldUrl = new Uri("http://localhost:1973/Services.aspx?idProject=10&idService=14");
Uri newUrl = oldUrl.SetParameter("idProject", "12").SetParameter("idService", "7");
Run Code Online (Sandbox Code Playgroud)


Bob*_*ich 8

C#HttpUtility.ParseQueryString实用程序将为您完成繁重的工作.您将需要在最终版本中执行更强大的空值检查.

    // Let the object fill itself 
    // with the parameters of the current page.
    var qs = System.Web.HttpUtility.ParseQueryString(Request.RawUrl);

    // Read a parameter from the QueryString object.
    string value1 = qs["name1"];

    // Write a value into the QueryString object.
    qs["name1"] = "This is a value";
Run Code Online (Sandbox Code Playgroud)


Gia*_*rdi 6

这是我的实现:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一些示例:

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
Run Code Online (Sandbox Code Playgroud)