1

Just as an exercise I'd like to create an ASP.Net web API which converts between XML and JSON.

this is what I have so far:

enter image description here

I think the content type checking is right, but I can't test as I am getting "Method not Allowed" when I try and post, is it possible to have routing/binding to achieve my goal?

I am trying to test this with Fiddler: enter image description here

my routes look like this: enter image description here

my webapi routes look like this: enter image description here but I still get Method Not Allowed :=/ enter image description here

GreyCloud
  • 3,030
  • 5
  • 32
  • 47
  • 1
    How are you calling this action? Could you show your client code? Also please show your current routes setup. – Darin Dimitrov Oct 16 '13 at 14:20
  • @DarinDimitrov I've added this info :) – GreyCloud Oct 16 '13 at 14:25
  • No, you have shown your MVC routes setup not your Web API routes. Those are 2 completely different things. Also you cannot expect to get the POST request payload into a variable called `jsonOrXml` in your Post action. You will have to read it from the request body. It won't come as parameter. Or if you want it to you will have to write a custom model binder or formatter. – Darin Dimitrov Oct 16 '13 at 14:35
  • @DarinDimitrov sorry, where are we webapi routes? how can I load from the request body? – GreyCloud Oct 16 '13 at 14:45
  • By default, when you create a new application using the wizard they are placed in `~/App_Start/WebApiConfig.cs`. Also from your Fiddler screenshot I can see that you have provided invalid JSON payload: `{sample:"true"}`. A valid JSON should look like this: `{"sample":"true"}`. – Darin Dimitrov Oct 16 '13 at 14:58
  • thanks, I posted the WebAPIConfig and fixed the json, still the same result – GreyCloud Oct 16 '13 at 15:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39365/discussion-between-greycloud-and-darin-dimitrov) – GreyCloud Oct 16 '13 at 15:01
  • Could you show the response you get from the server in Fiddler? – Darin Dimitrov Oct 16 '13 at 15:02

1 Answers1

2

Try annotating your action parameter with FromBodyAttribute. Here's an example:

public class TestController : ApiController
{
  public string Post([FromBody] string jsonOrXml)
  {
    // Process the input
  }
}

In this case the Content-Type request header must be application/x-www-form-urlencoded and the body will have the following format: =[JSON or XML data]. More details are given here.

Hope this helps.

Community
  • 1
  • 1
volpav
  • 5,090
  • 19
  • 27
  • Thank you, this resolves the Method Not Found error, however the "jsonOrXml" variable is null – GreyCloud Oct 16 '13 at 15:14
  • @GreyCloud I've updated my answer (added a helpful link to another SO question). – volpav Oct 16 '13 at 15:23
  • thankyou, this looks promising, it means i'd have to wrap any json or xml e.g. {"toconvert": {"sample":"1"}} so that this would match a model in MVC – GreyCloud Oct 16 '13 at 15:31
  • i will try this out tomorrow, in the mean time i wonder if it is possible to do this without wrapping? – GreyCloud Oct 16 '13 at 15:32
  • @GreyCloud You don't have to wrap anything as long as you use `application/x-www-form-urlencoded` as content type. Otherwise, you need a model type that you want the POST data (either JSON or XML string) to be mapped to (plus, the correct request type). – volpav Oct 16 '13 at 15:34