簡體   English   中英

ASP.NET C#序列化HttpValueCollection

[英]ASP.NET C# serialize HttpValueCollection

我正在嘗試序列化Request對象以進行記錄。 編碼

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
// obj is a Request object

給我以下異常:

To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy. System.Web.HttpValueCollection does not implement Add(System.String).

如何解決問題? 謝謝。

簡而言之,嘗試序列化http請求對象可能會失敗。 即使您解決了當前問題,我也希望它在更多地方失敗。

您應該以簡單的形式構造自己的對象模型,其中包括您關心的請求的那些部分。 對於HttpValueCollection,您可能需要添加某種類型的基本集合,即名稱/值對。

然后:從實際請求中填充新模型,並序列化模型。

我沒有嘗試過,但這可能會起作用並且可以序列化。

HttpContext.Current.Request.GetType()
                .GetProperties()
                .Select(
                    a =>
                        new KeyValuePair<object, object>(a, HttpContext.Current.Request.GetType().GetProperty(a.GetType().Name)))
                .ToList();

如果您對整個請求(即字節)感興趣,則可以使用HttpRequest.Filter屬性 它允許安裝可以從原始輸入HTTP請求讀取和寫入的過濾器(從Stream派生的對象)。

這是有關此主題的文章: 使用.NET過濾HTTP請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM