栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将XML作为POST传递给ASP MVC .NET中的ActionResult

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将XML作为POST传递给ASP MVC .NET中的ActionResult

这可以通过使用ActionFilterAttribute来完成。动作过滤器基本上在动作结果之前或之后与请求相交。因此,我只是为POST操作结果构建了一个自定义操作过滤器属性。这是我所做的:

public class RestAPIAttribute : ActionFilterAttribute{    public override void onActionExecuting(ActionExecutingContext filterContext)    {        HttpContextbase httpContext = filterContext.HttpContext;        if (!httpContext.IsPostNotification)        { throw new InvalidOperationException("only POST messages allowed on this resource");        }        Stream httpBodyStream = httpContext.Request.InputStream;        if (httpBodyStream.Length > int.MaxValue)        { throw new ArgumentException("HTTP InputStream too large.");        }        int streamLength = Convert.ToInt32(httpBodyStream.Length);        byte[] byteArray = new byte[streamLength];        const int startAt = 0;                httpBodyStream.Read(byteArray, startAt, streamLength);                StringBuilder sb = new StringBuilder();        for (int i = 0; i < streamLength; i++)        { sb.Append(Convert.ToChar(byteArray[i]));        }        string xmlBody = sb.ToString();        //Sends XML Data To Model so it could be available on the ActionResult        base.onActionExecuting(filterContext);    }}

然后在控制器上的操作结果方法上,您应该执行以下操作:

    [RestAPIAttribute]    public ActionResult MyActionResult()    {        //Gets XML Data From Model and do whatever you want to do with it    }

希望这对其他人有帮助,如果您认为还有其他更优雅的方法,请告诉我。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号