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

8
  • 1
    How are you calling this action? Could you show your client code? Also please show your current routes setup. Commented Oct 16, 2013 at 14:20
  • @DarinDimitrov I've added this info :) Commented Oct 16, 2013 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. Commented Oct 16, 2013 at 14:35
  • @DarinDimitrov sorry, where are we webapi routes? how can I load from the request body? Commented Oct 16, 2013 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"}. Commented Oct 16, 2013 at 14:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, this resolves the Method Not Found error, however the "jsonOrXml" variable is null
@GreyCloud I've updated my answer (added a helpful link to another SO question).
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
i will try this out tomorrow, in the mean time i wonder if it is possible to do this without wrapping?
@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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.