如何将 XML 作为 POST 传递到 ASP MVC .NET 中的 ActionResult
我正在尝试为我的 ASP MVC 项目提供一个简单的 RESTful API。 我无法控制此 API 的客户端,它们将通过 POST 方法传递 XML,该方法将包含在服务器端执行某些操作所需的信息,并返回带有操作结果的 XML。 我在发回 XML 时没有遇到问题,问题是通过 POST 接收 XML。 我看过一些 JSON 示例,但由于我不会控制我的客户端(从我的角度来看,它甚至可能是 telnet),我认为 JSON 不起作用。 我对么?
我见过这样的例子:客户端简单地构建正确的表单格式作为请求正文的一部分,然后 ASP 解析消息,数据以 FormCollection 形式提供(?param1=value1¶m2=value2&等)。 但是,我想将纯 XML 作为消息正文的一部分传递。
感谢您的帮助,
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?
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¶m2=value2&,etc). However, I want to pass pure XML as part of the message body.
thanks for your help,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
@Freddy - 喜欢你的方法,并使用以下代码对其进行了改进,以简化流读取:
然后在控制器中,你可以将 xml 作为字符串访问:
@Freddy - liked your approach and improved on it with the following code to simplify stream reading:
Then in the Controller you can access the xml as a string:
这可以通过使用 ActionFilterAttribute 来完成。 操作过滤器基本上在操作结果之前或之后与请求相交。 所以我刚刚为 POST Action Result 构建了一个自定义操作过滤器属性。 这就是我所做的:
然后在控制器上的操作结果方法上,您应该执行如下操作:
希望这对其他人有帮助,如果您认为有更优雅的方法可以做到这一点,请告诉我。
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:
Then on the action result method on your controller you should do something like this:
Hope this helps somebody else, if you think there are more elegant ways to do it, let me know.
为什么他们不能在表单帖子中将 xml 作为字符串传递?
示例:
您可以创建表单帖子并将其发送到单个表单字段中。
Why can they not pass the xml as a string in the form post?
Example:
You could create a form post and send it in a single form field.
我知道您可以创建一个自定义价值提供者工厂。 这还可以让您在模型发布时验证模型,然后再尝试保存它们。 Phil Haack 有一篇关于同一概念的 JSON 版本的博客文章。 唯一的问题是我不知道如何为 XML 实现同样的事情。
I know you can create a custom value provider factory. This will let you also validate your models when they are posted before attempting to save them. Phil Haack has a blog post about a JSON version of this same concept. The only problem is that I don't know how to implement one this same sort of thing for XML.
IMO 实现此目的的最佳方法是编写一个自定义值提供程序,这是一个处理请求到表单字典的映射的工厂。 您只需继承 ValueProviderFactory 并处理类型为“text/xml”或“application/xml”的请求。
更多信息:
菲尔·哈克
我的博客
MSDN
XmlValueProviderFactory
IMO the best way to accomplish this is to write a custom value provider, this is a factory that handles the mapping of the request to the forms dictionary. You just inherit from ValueProviderFactory and handle the request if it is of type “text/xml” or “application/xml.”
More Info:
Phil Haack
My blog
MSDN
XmlValueProviderFactory
我喜欢@Freddy 的回答和@Bowerm 的改进。 它简洁并保留了基于表单的操作的格式。
但 IsPostNotification 检查在生产代码中不起作用。 它不会像错误消息似乎暗示的那样检查 HTTP 谓词,并且当编译调试标志设置为 false 时,它会从 HTTP 上下文中剥离。 这里解释一下:
编译调试为 false 时 HttpContext.IsPostNotification 为 false
我希望这可以节省某人 1/2 天的调试路由的时间。 这是没有该检查的解决方案:
I like the answer from @Freddy and improvement from @Bowerm. It is concise and preserves the format of form-based actions.
But the IsPostNotification check will not work in production code. It does not check the HTTP verb as the error message seems to imply, and it is stripped out of HTTP context when compilation debug flag is set to false. This is explained here:
HttpContext.IsPostNotification is false when Compilation debug is false
I hope this saves someone a 1/2 day of debugging routes due to this problem. Here is the solution without that check:
很好!,
我在控制器方法中获得了什么对象来操作 Xml?
我使用这种方式:
在 actionFilter 上,我用以下内容填充模型:
在我的控制器方法上,我得到的模型为:
这是正确的实现吗?
谢谢。
Nice!,
What object I got in my controller method to manipulate the Xml?
I'm using this way:
On actionFilter, I populate the model with:
And on my controller method, I get the Model as:
Is this a correct implementation?
Thanks.