2013-02-13 37 views
0

比方說你有一個對象通JSON上的Response.Redirect

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; } 
    } 

,現在我想給模型傳遞從ASP.NET應用程序查詢字符串到ASP.NET MVC應用程序

string json = JsonConvert.SerializeObject(someModelVM); 
//how to convert it to querystring ? 
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + querystring, true); 

所以重定向後會綁定正確

public ActionResult SomeAction(SomeViewModel someViewModel) 
{ 
//do something here 
} 

UPDATE

我選擇了簡單的解決方案,而不是複雜化。

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 
    } 
+2

請記住,瀏覽器使用「GET」方法可以通過HTTP發送多少數據的限制。 – Matthew 2013-02-13 01:48:43

+0

謝謝,我知道。 – 2013-02-13 02:07:56

回答

1

你可以看到this例子類爲「查詢字符串」轉換

例如,您擁有的課程必須是這樣的:

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 

,那麼你必須用新的文本來連接你的網址:

var someViewModel = new ToQueryString { Name = "My name", ... }; 
var querystring = someViewModel.ToQueryString(); 
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + querystring, true); 

你不需要HttpUtility.UrlEncode因爲分機已處理做到這一點。

編輯 for @Matthew評論。如果你有一個大的查詢字符串,你可以使用這樣的列表,以拉鍊查詢字符串的工具,然後contatenate值:

c-compress-and-decompress-strings
compression-decompression-string-with-c-sharp

在這種情況下,你可以使用JSON格式,已經是您發送變量中的文本的zip。但是,您需要更改,然後接收此參數動作:

Response.Redirect("http://somedomain.com/SomeAction?redirectZip=" + jsonStringZip, true); 

代碼從this博客:

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())))); 
    } 
} 
+0

你的回答是最詳細的,但提供的轉換器不適用於複雜類型,我仍然會贊成它。謝謝。 – 2013-02-13 18:33:59

+0

我想你需要的是另一個問題,「如何將一個'Object'轉換爲'QueryString'?」 – 2013-02-13 20:04:29

0

試試這個:

string json = JsonConvert.SerializeObject(someModelVM); 
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + HttpUtility.UrlEncode(json), true); 

它使用UrlEncode method編碼的JSON是URL的一部分作爲查詢字符串

+0

我需要如何從對象或json中獲取查詢字符串。再次閱讀問題。 – 2013-02-13 02:06:26

+0

@plurby:當然!在我的答案中使用正確的變量進行更改 – 2013-02-13 02:08:50

+0

這不起作用,因爲Json只是編碼,並且不會轉換爲Param = Value查詢字符串。 – 2013-02-13 18:31:13

0

我認爲你需要使用HttpUtility.ParseQueryString用於此目的。

檢查從msdn文檔和Stackoverflow

此線程希望它能幫助。