Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
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
show 4 more comments

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.