1

I have classic asp project developed which is returning XML. Now I need to post that XML to ASP.NET WebAPI.

Below is the code for Classic ASP:

Dim testString 
Dim xmlHTTP
Dim postUrl
Dim responseXML
Dim responseText
testString = "This is a test string"
postUrl = "http://localhost:55823/api/order/"
responseText = ""

Set xmlHTTP = server.Createobject("MSXML2.XMLHTTP")
xmlHTTP.Open "POST", postUrl, false
xmlHTTP.setRequestHeader "Content-Type","application/x-www-form-urlencoded"

On Error Resume Next

xmlHTTP.send testString
response.Write testString

Below is how I am calling in WebAPI :

[HttpPost]
public HttpResponseMessage Post([FromBody]string testString)
{

}

But here testString is always returning Null. I even tried public string Post().

Can someone help me in this!!

user692942
  • 16,398
  • 7
  • 76
  • 175
Ajay
  • 317
  • 2
  • 12
  • 25
  • Is it actually triggering your controller, have you checked the routing? Your WebAPI code to too basic, please include the class (Controller) this call belongs to. – user692942 Apr 22 '14 at 10:59
  • @Lankymart Yes It is triggering controller. I can check in debug mode. My controller class is too big so I dint posted my controller code. And also since testString paramter is showing null everytime, I dont find reason to post code. Am i right in this?? For testing purpose I am not doing anything in my controller. I am just passing string and trying to take that string in testString parameter which I am not able to do that. – Ajay Apr 22 '14 at 11:09
  • Shouldn't you check xmlHTTP.responseText in that last response.write ? – AardVark71 Apr 22 '14 at 11:19
  • @AardVark71 xmlHTTP.responseText is response which I get from WebAPI. That is, xml result returning from WebAPI to classic asp. Now it will be null. I need to take xml string from asp and then return responseText from this. So I cannot use xmlHTTP.responseText. – Ajay Apr 22 '14 at 11:25
  • @Ajay No I understand Controller code can be massive (although I'm a firm believer in [Fat Models Skinny Controllers](http://stackoverflow.com/a/14045514/692942)) I just meant at least include the class name so the method had some context. – user692942 Apr 22 '14 at 11:35
  • 1
    possible duplicate of [ASP.NET web api cannot get application/x-www-form-urlencoded HTTP POST](http://stackoverflow.com/questions/20369361/asp-net-web-api-cannot-get-application-x-www-form-urlencoded-http-post) – user692942 Apr 22 '14 at 11:39
  • 2
    I think you will find your answer [here](http://stackoverflow.com/a/20370629/692942). It's because the ASP.Net Web API is expecting your request in a particular format and because your `testString` doesn't fit this format it's being ignored. In your case your it should be `testString = "=This is a test string"` (Specifically, the name portion of the name/value pair must be empty for a simple type.). – user692942 Apr 22 '14 at 11:56
  • This post might help too: http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/ Looks like you should prefix your value to be sent over with an equal sign. E.g. "= This is a test string" – AardVark71 Apr 22 '14 at 12:38

1 Answers1

1

As you guys said, I used

 testString = "=This is a test string"

and it is working fine. Since no one posted this as answer, I only posting thinking could help others.

But credit should goes to Lankymart and others.:)

Community
  • 1
  • 1
Ajay
  • 317
  • 2
  • 12
  • 25