如何更新C#查询字符串? [英] How to update querystring in C#?

查看:135
本文介绍了如何更新C#查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某处在url有一个与放大器; sortBy = 6。如何更新这&安培; sortBy = 4或放大器;一个按钮sortBy = 2点击?我需要编写自定义字符串函数创建正确的重定向URL?

Somewhere in the url there is a &sortBy=6 . How do I update this to &sortBy=4 or &sortBy=2 on a button click? Do I need to write custom string functions to create the correct redirect url?

如果我只需要追加一个查询字符串变量,我会做

If I just need to append a query string variable I would do

string completeUrl = HttpContext.Current.Request.Url.AbsoluteUri + "&" + ...
Response.Redirect(completeUrl);

但我想要做的是修改现有的查询字符串变量。

But what I want to do is modify an existing querystring variable.

推荐答案

要修改现有的查询字符串值,采用这种方式:

To modify an existing QueryString value use this approach:

var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
nameValues.Set("sortBy", "4");
string url = Request.Url.AbsolutePath;
string updatedQueryString = "?" + nameValues.ToString();
Response.Redirect(url + updatedQueryString);

我去到另一个响应。

这篇关于如何更新C#查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆