ASP.NET MVC XML绑定Action参数列表
昨天查看了 ASP.NET MVC 的生命周期,并沒有找到類似的解決方案。
?
不過今天在 stackoverflow上找到了解決方案,沒耐心的同學可以直接戳原文拷貝代碼,原文地址:How to pass XML as POST to an ActionResult in ASP MVC .NET
?
看了外國同學的說明,才發現 MVC居然可以根據不同的請求 Content Type來選擇 ValueProvider,這樣提供了很好的擴展。
MVC本身提供了 JsonValueProviderFactory,顧名思義就是為 Json服務的。那么在這里我們需要提供為 XML服務的ValueProviderFactory。
?
第一步:創建 XMLValueProviderFactory
public class XmlValueProviderFactory : ValueProviderFactory{private static void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, XElement xmlDoc){// Check the keys to see if this is an array or an objectvar uniqueKeys = new List<string>();int totalCount = 0;foreach (XElement element in xmlDoc.Elements()){if (!uniqueKeys.Contains(element.Name.LocalName))uniqueKeys.Add(element.Name.LocalName);totalCount++;}bool isArray;if (uniqueKeys.Count == 1){isArray = true;}else if (uniqueKeys.Count == totalCount){isArray = false;}else{// Not sure how to deal with a XML doc that has some keys the same, but not all// For now don't process this nodereturn;}// Add the elements to the backing storeint elementCount = 0;foreach (XElement element in xmlDoc.Elements()){if (element.HasElements){if (isArray){// Omit local name for arrays and add index insteadAddToBackingStore(backingStore, $"{prefix}[{elementCount}]", element);}else{AddToBackingStore(backingStore, MakePropertyKey(prefix, element.Name.LocalName), element);}}else{backingStore.Add(MakePropertyKey(prefix, element.Name.LocalName), element.Value);}elementCount++;}}private static XDocument GetDeserializedXml(ControllerContext controllerContext){var contentType = controllerContext.HttpContext.Request.ContentType;if (!contentType.StartsWith("text/xml", StringComparison.OrdinalIgnoreCase)&& !contentType.StartsWith("application/xml", StringComparison.OrdinalIgnoreCase)){// Are there any other XML mime types that are used? (Add them here)// not XML requestreturn null;}XDocument xml;try{// DTD processing disabled to stop XML bomb attack - if you require DTD processing, read this first: http://msdn.microsoft.com/en-us/magazine/ee335713.aspxvar xmlReaderSettings = new XmlReaderSettings {DtdProcessing = DtdProcessing.Prohibit};var xmlReader = XmlReader.Create(controllerContext.HttpContext.Request.InputStream, xmlReaderSettings);xml = XDocument.Load(xmlReader);}catch (Exception){return null;}if (xml.FirstNode == null){// No XML datareturn null;}return xml;}private static string MakeArrayKey(string prefix, int index){return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";}private static string MakePropertyKey(string prefix, string propertyName){return (prefix.IsNullOrEmpty()) ? propertyName : prefix + "." + propertyName;}#region ValueProviderFactory Memberspublic override IValueProvider GetValueProvider(ControllerContext controllerContext){var xmlData = GetDeserializedXml(controllerContext);if (xmlData == null)return null;var backingStore = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);AddToBackingStore(backingStore, String.Empty, xmlData.Root);return new DictionaryValueProvider<object>(backingStore, CultureInfo.CurrentCulture);}#endregion}?
該段代碼屬于網上抄襲,基本沒有改,在下覺得這樣已經滿足需求了,原文地址:Sending XML to an ASP.NET MVC Action Method Argument
PS:如果涉及版權問題,請告知在下。
?
第二步:在 Global.asmx.cs 的 Application_Start 方法中注冊 ValueProviderFactory
?ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); ValueProviderFactories.Factories.Add(new XmlValueProviderFactory());??
親測可用,如有問題,請留言。
轉載于:https://www.cnblogs.com/Currention/p/5169597.html
總結
以上是生活随笔為你收集整理的ASP.NET MVC XML绑定Action参数列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql max_allowed_pa
- 下一篇: vc6.0连接mysql数据库