试图建立一个控制器动作为XML的端点 [英] Trying to set up a controller action as Xml endpoint

查看:174
本文介绍了试图建立一个控制器动作为XML的端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用ASP.NET MVC 2的控制器操作建立一个POX端点。我想我将能够重复使用了很多的工作code,并在15分钟上衣来完成。我错了。
我的动作看起来是这样的:

I wanted to build a POX endpoint by using an ASP.NET MVC 2 controller action. I thought I would be able to reuse a lot of working code and be done in 15 min tops. I was wrong. My action looks like this:

[HttpPost]
[ValidateInput(false)]
public ContentResult DoSomething(string xml)

该ValidateInput属性是必需的,因为你发布的XML操作时得到一个讨厌的验证异常,否则。

The ValidateInput attribute is required because you'll get a nasty validation exception otherwise when posting Xml to the action.

客户端code:

var req = (HttpWebRequest)WebRequest.Create(endPoint);
req.Method = "POST";
req.ContentType = "text/xml";
req.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
// ...open request, read response now

这是<强>工作code 。下面的请求被发送到端点(fiddler2)

This is working code. The following request is sent to the endpoint (fiddler2)

POST http://doerak/Veekijker/Service.aspx/Melding HTTP/1.1
Content-Type: text/xml
Accept-Encoding: gzip,deflate,sdch
Host: theHost
Content-Length: 2220
Expect: 100-continue
Connection: Keep-Alive

Xml=theXml

然而,当我删除了接受编码,从客户端code头,我控制器操作的XML字符串参数为null。

However, when I remove the "Accept-Encoding" header from the client code, the xml string parameter of my controller action is null.

无需编码接受头请求看起来像这样

The request without the accept-encoding header looks like this

POST http://doerak/Veekijker/Service.aspx/Melding HTTP/1.1
Content-Type: text/xml
Host: theHost
Content-Length: 2220
Expect: 100-continue

Xml=theXml

如何使用我的控制器操作,而不必设置Accept-Encoding头在客户端上?

How can I use my controller action, without having to set the Accept-Encoding header on the client?

推荐答案

这肯定不是文本/ XML ,你在请求​​体发送。这是一个标准的形式的name = value

This for sure isn't text/xml that you're sending in the request body. It's a standard form name=value pair.

或者:


  • 真正发送的数据为XML BLOB(这可能需要一个属性的方法)

  • 或使用标准内容类型所有的HTML表单使用(这是应用程序/ x-WWW的形式urlen codeD ),并逃避与URL编码值。

  • really send the data as XML blob (and that maybe needs an attribute at the method)
  • or use the standard Content-Type that all HTML forms use (which is application/x-www-form-urlencoded), and escape the value with URL encoding.

这篇关于试图建立一个控制器动作为XML的端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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