Armand
Armand

Reputation: 10264

ASP.NET Web API multiple post messages single controller or action

So I have the following issue:

We have a scenario where a third party is sending post messages to our server, we have no control over that, and they want to send everything to a single url, so we can not do anything regarding query parameters, so my question then is, is it possible to handle different post messages with a single action. In essence the content of the post message will define what needs to happen with said request.

Post message 1:

<xml>
    <content1> content </content1>
    <content2> content </content2>
</xml>

Post message 2:

<xml>
    <content1> content </content1>
    <content3> content </content3>
</xml>

As you can see some of the messages will have similar properties.

Thanks

Upvotes: 0

Views: 371

Answers (2)

Darrel Miller
Darrel Miller

Reputation: 142222

You can use the following signature to accept arbitrary XML content,

public HttpResponseMessage Post([FromBody]XElement xmlbody) { 
    // Process the xmlbody 
    return new HttpResponseMessage(HttpStatusCode.Created); 
} 

I have more details about this kind of raw media type processing here.

Upvotes: 2

leskovar
leskovar

Reputation: 661

It is possible. Specify your action to have a string input parametre. That way you can get the entire message as a string and not worry about its properties... However, you will need to implement the logic in the controller to diferentiate messages.

[HttpPost]
public ActionResult MyActionForAllMessages(string message)
{
    //Your logic
}

Upvotes: 0

Related Questions