0

I am making from a ApiController a direct SQL query (SQL Server 2014) to the DB:

    public object Post(JObject jsonData){
//..
 string qvalue = "SELECT s.Id, v.[Name] VehicleId, s.TimestampLocal FROM Status s INNER JOIN Vehicles v on s.VehicleId = v.Id";
     var resl = entities.Database.SqlQuery<StatusTime>(qvalue);
     return resl;

The response arrives to the client as Content-Type: "application/json; charset=utf-8". I want to change the Content-Type to something else like: "application/json" or even "text/xml".

I have tried adding before "return resl;" the line: HttpContext.Current.Response.ContentType = "application/json"; but it does not change it.

I have tried adding something to the header (just to see the response changed) and the header is changed (and arrives at the client changed) with:

HttpContext.Current.Response.AppendHeader("test", "hello");

I have seen this example: HttpContext.Current.Response.AddHeader() not setting Content-Type header

and indeed it sets the content type but I have no idea how to apply it to a SqlQuery result. Also I do not want to bring the data to the IIS server with .ToList or similar and then write it to the response because it is massive (~80MB).

I have seen this too: WebAPI to Return XML

and: Change Content-Type header for HttpResponseMessage

How could this be done, or is this decided for me by the controller?

Community
  • 1
  • 1
user3546827
  • 581
  • 1
  • 5
  • 15
  • The `charset` is probably there as a part of the response encoding. Does your config file specify a `responseEncoding`? – stephen.vakil Sep 16 '16 at 20:46
  • It may be constructive for you to say what you are ultimately trying to accomplish. What is the problem with the default content type for the return of an object? I am just wondering if you are asking the right question. – btberry Sep 16 '16 at 21:00
  • @btberry I am trying to fix this problem: http://stackoverflow.com/questions/12139957/jquery-ajax-response-undefined-with-internet-explorer The content is successfully downloaded in Chome, Firefox and even in IE (for a small dataset <15MB). After that IE downloads all the content (response 200 even for 100MB+) but the data is undefined. I hope changing the content-type will solve the issue. – user3546827 Sep 16 '16 at 21:31
  • @stephen.vakil no I did not specify a responseEncoding in web.config. One of the references in my post actually says that the type is set automatically by the apicontroller if i return a serializable object. – user3546827 Sep 16 '16 at 21:40

0 Answers0