C# 在响应时传递JSON。重定向
假设你有一个对象C# 在响应时传递JSON。重定向,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,假设你有一个对象 public class SomeViewModel { public SomeViewModel() { this.SomeData = new List<SomeData>(); } public string Name { get; set; } public string Surname{ get; set; } public L
public class SomeViewModel
{
public SomeViewModel()
{
this.SomeData = new List<SomeData>();
}
public string Name { get; set; }
public string Surname{ get; set; }
public List<SomeData> SomeData { get; set; }
}
public class SomeData
{
public string Name { get; set; }
public string Value { get; set; }
}
所以重定向后它会正确绑定
public ActionResult SomeAction(SomeViewModel someViewModel)
{
//do something here
}
更新
我选择了简单的解决方案,而不是复杂化
string json = JsonConvert.SerializeObject(someModelVM);
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + json, true);
public ActionResult SomeAction(string json)
{
//try to deserialize json
//security check the json
//do stuff
}
试试这个:
string json = JsonConvert.SerializeObject(someModelVM);
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + HttpUtility.UrlEncode(json), true);
这使用将JSON编码为URL的一部分作为querystring您可以看到将类转换为“查询字符串”的示例
例如,对于您拥有的类,必须是这样的:
Name = My name
Surname = My Surname
SomeData = [
{
Name = My SD0Name,
Value = My SD0Value
},
{
Name = My SD1Name,
Value = My SD1Value
}
]
Name=My%20name&Surname=My%20Surname&SomeData[0].Name=My%20SD0Name&SomeData[0].Value=My%20SD0Value&SomeData[1].Name=My%20SD1Name&SomeData[1].Value=My%21SD0Value
然后,您必须将url与新文本连接起来:
var someViewModel = new ToQueryString { Name = "My name", ... };
var querystring = someViewModel.ToQueryString();
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + querystring, true);
您不需要HttpUtility.UrlEncode
,因为扩展已经在处理此操作
编辑@Matthew comment的。如果您有一个较大的查询字符串,您可以使用类似于此列表的工具压缩查询字符串,然后添加一个值:
在本例中,您可以使用Json格式,即发送变量中文本的zip。但您需要更改接收此参数的操作:
Response.Redirect("http://somedomain.com/SomeAction?redirectZip=" + jsonStringZip, true);
来自博客的代码:
公共静态类UrlHelpers
{
公共静态字符串ToQueryString(此对象请求,字符串分隔符=“,”)
{
if(请求==null)
抛出新的ArgumentNullException(“请求”);
//获取对象上的所有属性
var properties=request.GetType().GetProperties()
.其中(x=>x.CanRead)
.Where(x=>x.GetValue(请求,null)!=null)
.ToDictionary(x=>x.Name,x=>x.GetValue(请求,null));
//获取所有IEnumerable属性的名称(不包括字符串)
var propertyNames=属性
其中(x=>!(x.Value是字符串)和&x.Value是IEnumerable)
.选择(x=>x.Key)
.ToList();
//将所有IEnumerable属性合并为逗号分隔的字符串
foreach(propertyNames中的var键)
{
var valueType=properties[key].GetType();
var valuelemtype=valueType.IsGenericType
?valueType.GetGenericArguments()[0]
:valueType.GetElementType();
if(valueElemType.IsPrimitive | | valueElemType==类型(字符串))
{
var enumerable=属性[键]为IEnumerable;
properties[key]=string.Join(分隔符,enumerable.Cast());
}
}
//将所有键/值对合并为一个字符串,用符号AND分隔
返回字符串。Join(&),属性
.Select(x=>string.Concat(
Uri.EscapeDataString(x.Key),“=”,
EscapeDataString(x.Value.ToString());
}
}
我认为您需要使用HttpUtility.ParseQueryString
来实现此目的
检查中的文档和中的此线程
希望有帮助。请记住,浏览器对使用“GET”方法通过HTTP发送的数据量有限制。我需要如何从object或json获取querystring。把问题再读一遍。@plurby:当然!在我的答案中更改为使用正确的变量这不起作用,因为Json只是编码的,并且没有转换为Param=Value查询字符串。您的答案是最详细的,但提供的转换器不适用于复杂类型,我仍将对其进行升级。谢谢。我想你需要的是另一个问题,“如何将
对象
转换为查询字符串
?”
Response.Redirect("http://somedomain.com/SomeAction?redirectZip=" + jsonStringZip, true);
public static class UrlHelpers
{
public static string ToQueryString(this object request, string separator = ",")
{
if (request == null)
throw new ArgumentNullException("request");
// Get all properties on the object
var properties = request.GetType().GetProperties()
.Where(x => x.CanRead)
.Where(x => x.GetValue(request, null) != null)
.ToDictionary(x => x.Name, x => x.GetValue(request, null));
// Get names for all IEnumerable properties (excl. string)
var propertyNames = properties
.Where(x => !(x.Value is string) && x.Value is IEnumerable)
.Select(x => x.Key)
.ToList();
// Concat all IEnumerable properties into a comma separated string
foreach (var key in propertyNames)
{
var valueType = properties[key].GetType();
var valueElemType = valueType.IsGenericType
? valueType.GetGenericArguments()[0]
: valueType.GetElementType();
if (valueElemType.IsPrimitive || valueElemType == typeof (string))
{
var enumerable = properties[key] as IEnumerable;
properties[key] = string.Join(separator, enumerable.Cast<object>());
}
}
// Concat all key/value pairs into a string separated by ampersand
return string.Join("&", properties
.Select(x => string.Concat(
Uri.EscapeDataString(x.Key), "=",
Uri.EscapeDataString(x.Value.ToString()))));
}
}