欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > asp.net >内容正文

asp.net

asp.net mvc xml绑定action参数列表 -凯发k8官方网

发布时间:2025/1/21 asp.net 26 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 asp.net mvc xml绑定action参数列表 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

昨天查看了 asp.net mvc 的生命周期,并没有找到类似的凯发k8官方网的解决方案。

 

不过今天在 stackoverflow上找到了凯发k8官方网的解决方案,没耐心的同学可以直接戳原文拷贝代码,原文地址: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:如果涉及凯发k8官方网的版权问题,请告知在下。

 

第二步:在 global.asmx.cs 的 application_start 方法中注册 valueproviderfactory

 valueproviderfactories.factories.add(new jsonvalueproviderfactory()); valueproviderfactories.factories.add(new xmlvalueproviderfactory()); 

 

亲测可用,如有问题,请留言。

转载于:https://www.cnblogs.com/currention/p/5169597.html

总结

以上是凯发k8官方网为你收集整理的asp.net mvc xml绑定action参数列表的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图