ASP .NET Web API返回JSON文件

时间:2016-07-29 22:23:55

标签: c# asp.net asp.net-mvc asp.net-web-api

我正在创建一个ASP.NET Web API服务端点,它在该端点查询Oracle数据库并以JSON格式返回结果。

以下是我在Controller中使用的代码

public class SampleController : ApiController
{
  public HttpResponseMessage Getdetails([FromUri] string[] id)
  {
    using (OracleConnection dbconn = new OracleConnection("DATA SOURCE=J;PASSWORD=C;PERSIST SECURITY INFO=True;USER ID=T"))
    {
        var inconditions = id.Distinct().ToArray();
        var srtcon = string.Join(",", inconditions);
        DataSet userDataset = new DataSet();
        var strQuery = @"SELECT * from STCD_PRIO_CATEGORY where STPR_STUDY.STD_REF IN(" + srtcon + ")";
        OracleCommand selectCommand = new OracleCommand(strQuery, dbconn);
        OracleDataAdapter adapter = new OracleDataAdapter(selectCommand);
        DataTable selectResults = new DataTable();
        adapter.Fill(selectResults);
        string result = JsonConvert.SerializeObject(selectResults);
        string contentDisposition = "inline; filename=ProvantisStudyData.json";
        //byte[] byteInfo = Encoding.ASCII.GetBytes(result);
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, result, MediaTypeHeaderValue.Parse("application/json"));
        response.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);
        //response.Content.Headers.ContentLength = byteInfo.Length;
        return response;
 }
}

一切正常,但返回的结果是以下格式

  

" [{\" CATEGORY \":\"内   研究\" \" session_number事务\":7,\" SESSION_START_DATE \":\" 2015-02-13T00:00:00 \&# 34; \" SESSION_START_TIME \":\" 2015-02-13T10:33:59.288394 \" \" SESSION_END_DATE \":\&# 34; 2015-02-13T00:00:00 \" \" SESSION_END_TIME \":\" 2015-02-13T12:11:34 \"}] "

只是它创建了额外的引号和额外的转义字符(\)。我是否需要进行操作以将其移除。

  

[{" CATEGORY":"内   研究"" session_number事务":7," SESSION_START_DATE":" 2015-02-13T00:00:00"" SESSION_START_TIME" :" 2015-02-13T10:33:59.288394"" SESSION_END_DATE":" 2015-02-13T00:00:00""&SESSION_END_TIME #34;:" 2015-02-13T12:11:34"}]

1 个答案:

答案 0 :(得分:2)

这是因为您已经将数据序列化为JSON,然后尝试将其作为application/json返回,这只会导致格式化程序将您的字符串转换为JSON格式的字符串。

所以基本上你将对象序列化为json字符串

  

[{" CATEGORY":"内部研究"," SESSION_NUMBER":7," SESSION_START_DATE":" 2015- 02-13T00:00:00"" SESSION_START_TIME":" 2015-02-13T10:33:59.288394"" SESSION_END_DATE":&#34 ; 2015-02-13T00:00:00"" SESSION_END_TIME":" 2015-02-13T12:11:34"}]

然后将json字符串序列化为序列化的json字符串

  

" [{\" CATEGORY \":\"内   研究\" \" session_number事务\":7,\" SESSION_START_DATE \":\" 2015-02-13T00:00:00 \&# 34; \" SESSION_START_TIME \":\" 2015-02-13T10:33:59.288394 \" \" SESSION_END_DATE \":\&# 34; 2015-02-13T00:00:00 \" \" SESSION_END_TIME \":\" 2015-02-13T12:11:34 \"}] "

移除..

string result = JsonConvert.SerializeObject(selectResults);

...只需将selectResults对象原样传递给响应,格式化程序将根据媒体类型完成剩下的工作。

//...other code removed for brevity

var response = Request.CreateResponse(HttpStatusCode.OK, selectResults, MediaTypeHeaderValue.Parse("application/json"));
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("inline; filename=ProvantisStudyData.json", out contentDisposition)) {
    response.Content.Headers.ContentDisposition = contentDisposition;
}
return response;

例如,如果您将内侧类型更改为xml,则响应将作为xml返回。