如何通过XML作为POST在ASP .NET MVC一个ActionResult [英] How to pass XML as POST to an ActionResult in ASP MVC .NET

查看:144
本文介绍了如何通过XML作为POST在ASP .NET MVC一个ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个简单的RESTful API,以我的ASP MVC项目。我不会有此API的客户端的控制下,它们将通过POST方法,将包含执行在服务器端的一些动作,并提供背部与动作的结果的XML所需的信息被传递的XML。我没有发回XML的问题,这个问题是通过POST接收XML。我见过一些JSON的例子,但因为我不会管我的客户(可以是甚至从我的角度来看一个telnet),我不认为JSON会工作。我对么?

I am trying to provide a simple RESTful API to my ASP MVC project. I will not have control of the clients of this API, they will be passing an XML via a POST method that will contain the information needed to perform some actions on the server side and provide back an XML with the result of the action. I don't have problems sending back XMLs, the problem is receiving XML via a POST. I have seen some JSON examples, but since I will not control my clients (it could be even a telnet from my point of view) I don't think JSON will work. Am I correct?

我所看到的例子,客户只需构造正确的表格格式的请求体的一部分,然后在ASP解析消息和数据可作为的FormCollection(参数1 =值1&放大器;参数2 =值2和放大器;等等) 。不过,我想通过纯XML作为邮件正文的一部分。

I have seen examples where clients simply construct the correct form format as part of the body of the request and then the ASP parse the message, and data is available as FormCollection (?param1=value1&param2=value2&,etc). However, I want to pass pure XML as part of the message body.

感谢您的帮助,

推荐答案

这可以通过使用ActionFilterAttribute完成。操作过滤器基本上前或操作结果后相交的请求。所以,我只是专为POST操作结果的自定义操作筛选器属性。下面是我所做的:

This could be accomplished by using the ActionFilterAttribute. Action Filters basically intersects the request before or after the Action Result. So I just built a custom action filter attribute for POST Action Result. Here is what I did:

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;

        /*
         * Copies the stream into a byte array
         */
        httpBodyStream.Read(byteArray, startAt, streamLength);

        /*
         * Convert the byte array into a string
         */
        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);
    }
}

然后在您的控制器上的操作结果的方法,你应该做的是这样的:

Then on the action result method on your controller you should do something like this:

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

希望这有助于别人,如果你觉得有更优雅的方式来做到这一点,让我知道。

Hope this helps somebody else, if you think there are more elegant ways to do it, let me know.

这篇关于如何通过XML作为POST在ASP .NET MVC一个ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆