This is super weird. I have 3 types of jobs, each with their own routh that starts the work and their own route to get the result. But the code to return the generated object is the same for all three types of job.
For two of the job types the "conn.setRequestProperty("Accept", "application/xml");" works fine. I have one client setting it to JSON and it gets JSON, the other sets it to XML and gets XML. But for the third job type, it always comes back as JSON.
The controller:
[Route("v1/reports/{guid}")]
[Route("v2/document/{guid}")]
[HttpGet]
public IHttpActionResult Get(string guid)
{
IHttpActionResult result = CheckForReport(guid, RepositoryStatus.REQUEST_TYPE.DocGen, true);
if (Log.IsDebugEnabled)
Log.Debug($"Document Get({guid}) = {result}");
return result;
}
The code returning:
protected IHttpActionResult CheckForReport(string guid, RepositoryStatus.REQUEST_TYPE requestType, bool returnReport)
{
switch (reportStatus.RequestType)
{
// returns JSON or XML
case RepositoryStatus.REQUEST_TYPE.TagTree:
return Ok(Repository.GetTagTree(guid));
// returns JSON or XML
case RepositoryStatus.REQUEST_TYPE.Metrics:
return Ok(Repository.GetMetrics(guid));
// returns JSON regardless of Accept setting
case RepositoryStatus.REQUEST_TYPE.DocGen:
return Ok(Repository.GetReport(guid));
default:
return NotFound();
}
}
The object being returned:
Document GetReport(string guid);
The object itself:
[DataContract]
public class Document
{
/// <summary>
/// The unique identifier for this request.
/// </summary>
[DataMember]
public string Guid { get; set; }
[DataMember]
public byte[][] Pages { get; set; }
}
The only thing different about the Document object is it has a member that's "byte[][]".
The WebAPI request uses the HTTP Accept header to specify the data type,different browsers specify their favorite data types,so different browsers receive different data formats.You need to specify the HTTP Accept header.
Such code is no problem.
public async Task<ActionResult> Index()
{
string apiUrl = "https://localhost:44395/api/test";
string data = null;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
data = await response.Content.ReadAsStringAsync();
}
}
return Content(data);
}
Here is the result.
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Check the client side code that is setting the Accept header value and choose a valid MIME type. It seems that you want either 'application/xml' or 'application/json'.
If this is just a typo then all you know is what the logging thinks is happening. You don't know what the MVC method thinks is happening. Use the debugger - find out.
Based on the code you gave, I did not reproduce your problem. Can you give more detailed code?Or give part of the code that your client makes the request?
I saw the same problem in this link, you can refer to it.
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
How can I debug what ASP.NET is doing? Everything looks fine when I'm returning from my controller. And there's no code for me to step into on the return.
Found it. In the returned class there's a member object of another class. That class does not have a parameterless constructor. The JSON serializer handles that fine. The XML one does not and apparently in that case ASP.NET figures better to return JSON
that throw an exception.
Added the parameterless constructor and now it works fine.
Member
24 Points
247 Posts
ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 03, 2020 09:22 PM|david@windward.net|LINK
This is super weird. I have 3 types of jobs, each with their own routh that starts the work and their own route to get the result. But the code to return the generated object is the same for all three types of job.
For two of the job types the "conn.setRequestProperty("Accept", "application/xml");" works fine. I have one client setting it to JSON and it gets JSON, the other sets it to XML and gets XML. But for the third job type, it always comes back as JSON.
The controller:
The code returning:
The object being returned:
The object itself:
The only thing different about the Document object is it has a member that's "byte[][]".
So why is it forcing a JSON return?
Contributor
2720 Points
777 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 04, 2020 03:47 AM|YihuiSun|LINK
Hi, david
The WebAPI request uses the HTTP Accept header to specify the data type,different browsers specify their favorite data types,so different browsers receive different data formats.You need to specify the HTTP Accept header.
Such code is no problem.
public async Task<ActionResult> Index() { string apiUrl = "https://localhost:44395/api/test"; string data = null; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(apiUrl); client.DefaultRequestHeaders.Accept.Clear(); //client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml")); HttpResponseMessage response = await client.GetAsync(apiUrl); if (response.IsSuccessStatusCode) { data = await response.Content.ReadAsStringAsync(); } } return Content(data); }
Here is the result.
Best Regards,
YihuiSun
Participant
1620 Points
927 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 04, 2020 05:42 AM|PaulTheSmith|LINK
First step is to check what the request is asking for
Set a breakpoint on the first line of the controller method. Issue the request which surprises you. What is the value of
this.Request.Headers.Accept
Don't assume you know what it is. Use the debugger and find out.
Member
24 Points
247 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 04, 2020 01:36 PM|david@windward.net|LINK
Hi YihuiSum;
I already have it set to application/xml. And looking in the debugger, the header on the server shows the Accept is set to xml. Yet it returns JSON.
Member
24 Points
247 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 04, 2020 01:37 PM|david@windward.net|LINK
Hi Paul;
I have it logging all the headers on the server side. Accept is set to application/xml.
thanks - dave
Participant
1620 Points
927 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 04, 2020 11:31 PM|PaulTheSmith|LINK
Check the client side code that is setting the Accept header value and choose a valid MIME type. It seems that you want either 'application/xml' or 'application/json'.
If this is just a typo then all you know is what the logging thinks is happening. You don't know what the MVC method thinks is happening. Use the debugger - find out.
Contributor
2720 Points
777 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 05, 2020 07:05 AM|YihuiSun|LINK
Hi, david
Based on the code you gave, I did not reproduce your problem. Can you give more detailed code?Or give part of the code that your client makes the request?
I saw the same problem in this link, you can refer to it.
Best Regards,
YihuiSun
Member
24 Points
247 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 05, 2020 03:58 PM|david@windward.net|LINK
Hi YihuiSun;
It works correctly for me for all routes except 1. For that one, the vale returned from:
is:
??? - thanks - dave
Member
24 Points
247 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 05, 2020 04:01 PM|david@windward.net|LINK
Hi Paul;
How can I debug what ASP.NET is doing? Everything looks fine when I'm returning from my controller. And there's no code for me to step into on the return.
??? - thanks - dave
Member
24 Points
247 Posts
Re: ASP.NET Web API2, one route returns JSON regardless of Accept setting
Jun 23, 2020 11:17 PM|david@windward.net|LINK
Found it. In the returned class there's a member object of another class. That class does not have a parameterless constructor. The JSON serializer handles that fine. The XML one does not and apparently in that case ASP.NET figures better to return JSON that throw an exception.
Added the parameterless constructor and now it works fine.