Attempting to configure controller action as an Xml endpoint

I wanted to create a POX endpoint using the ASP.NET MVC 2 controller action. I thought I could reuse a lot of working code and do it in 15 minutes. I was wrong. My actions are as follows:

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

The ValidateInput attribute is required because you will receive an unpleasant validation exception, otherwise when sending the Xml to the action.

Client 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 

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 

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

A request without an accept-encoding header is as follows:

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

How can I use my controller action without setting the Accept-Encoding header on the client?

0
source share
1 answer

This is definitely not the text/xml that you send to the request body. This is the standard form name=value . Couple.

Or:

  • really sends data as an XML block (and a method attribute may be needed)
  • or use the standard Content-Type , which uses all HTML forms (which is application/x-www-form-urlencoded ), and avoid URL-encoded values.
+1
source

Source: https://habr.com/ru/post/890007/


All Articles