3

I have been struggling with this issue to get to the root of it but no luck yet. My WebAPI runs in its own app pool in IIS and the actions is suppose to return XML and JSON but its only returns JSON. What is more strange is that it works fine on other PC so my guess is that it must be something wrong with my IIS or that I am missing a configuration of some sort. It works perfectly fine if I use Visual Studio Server but not IIS.

API Config:

//Added to support .net1.1 clients
config.Formatters.XmlFormatter.UseXmlSerializer = true;
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;
config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());

Data Property:

/// <summary>
/// Unique Transaction ID
/// </summary>
private int TransactionID;
[XmlElement(Type = typeof(int))]
public int fTransactionID
{
    get{return TransactionID;}
    set{TransactionID = value;}
}

Request Header

// Create a WebRequest to the remote site
WebRequest myWebClient = WebRequest.Create(uri);
myWebClient.ContentType = "application/xml";
myWebClient.Method = method;

EDIT

API Method

/// <summary>
/// Return Rebate Reversal Transactions
/// </summary>
/// <param name="FromDate"></param>
/// <param name="ToDate"></param>
/// <param name="PracticeID"></param>
/// <returns></returns>
[AttributeRouting.Web.Http.GET("RebateReversalTransaction/{FromDate}/{ToDate}/{PracticeID}")]
public GetRebateReversalTransactionResponse GetReversalTransaction(int FromDate, int ToDate, int PracticeID)
{
    GetRebateReversalTransactionResponse reversalResponse = new GetRebateReversalTransactionResponse();
    service = new PPNRebateSystemService();
    reversalResponse = service.GetPracticeRebateReversalTransactions(FromDate, ToDate, PracticeID);

    return reversalResponse;
}
5
  • I assume that the ContentType of the request is application/xml? Commented Jul 17, 2014 at 8:09
  • Have you run a HTTP sniffer yet? Commented Jul 17, 2014 at 8:36
  • I use Postman on Chrome to test my api, even though I specify content type it still does not return xml Commented Jul 17, 2014 at 9:02
  • Have you tried setting the "Accept:application/xml" header on WebRequest to API? Commented Jul 17, 2014 at 9:02
  • @Balachandra yes I have and still no luck. I think the issue resides at IIS level Commented Jul 17, 2014 at 9:07

3 Answers 3

2

In Global.asax.cs add the following code to Application_Start. This will clear any Formatters that might be registered and then add back the XML formatter. I have tested this and it responds with XML even when the client sends a JSON accept header (Accept: application/json).

protected void Application_Start() 
{
    ...


    GlobalConfiguration.Configuration.Formatters.Clear();
    //Force XML responses on all requests
    GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter());
    //Force JSON responses on all requests
    //GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

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

2 Comments

Thanks for this it worked, how can I add JSON as well?
It would have been far more useful to comment on HOW you got it to work. I (and possibly others) have run into the same scenario - still trying to figure it out...
1

The way that you specify the type of content that you want to receive is via the Accept header, not the Content-Type header:

WebRequest myWebClient = WebRequest.Create(uri);
myWebClient.Headers.Add("Accept","application/xml");
myWebClient.Method = method;

Content-Type is used to indicate the type of object contained in the body of your message - which should almost always in fact be empty for a GET request1.


1You're allowed to have something in the body of a GET request, but it's not meant to have any effect on how the request is processed - the semantics of GET are based purely on the path - so I don't think anyone's ever come up with an actual use for this.

1 Comment

I have tried using Accept but the results still come through as JSON and not XML. I am using Postman on chrome to test my api
0

try to delete json formatter. This will give you just xml results. And test in IIS like this. Use this:

 config.Formatters.Remove(config.Formatters.JsonFormatter);

If its okay that means your problem is in formatters.

or you can give custom header in request inside web api method.

Request.Content.Headers.Add("Content-Type", "application/xml");

Dont forget to remove myWebclient.ContentType="application/xml"; Because we already define a content header for request in web api method.

2 Comments

can you edit this question with your web api method that you want to render in xml?
Still doesn't work. There must be an issue with my IIS. That is all I can think of.

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.