0

I am looking to set the name of the file that is being returned by the ASP .NET WEB API. Currently its just returns in the name of the parameters that is being passed in the URL. But what if I need then to be returned as ABC.JSON

public class NewTestController : ApiController
{
public string 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 STPR_STUDY 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);
        return JsonConvert.SerializeObject(selectResults);
}}}

I did see in other forums using Content-Disposition but I am not using HTTPResponse in code. How can this be done. Thanks

I tried like below

 OracleCommand selectCommand = new OracleCommand(strQuery, dbconn);
            OracleDataAdapter adapter = new     OracleDataAdapter(selectCommand);
            DataTable selectResults = new DataTable();
            adapter.Fill(selectResults);
            string result =  JsonConvert.SerializeObject(selectResults);
            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            response.Content = new StreamContent(result);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "Abc.JSON"
            };
     return(result);

But it throws the error in StreamContent saying,the best overloaded method match for has some invalid arguments in StreamConent

4
  • 1
    You returning string from your controller. You should use at least IHttpActionResult, but better HttpResponseMessage to set your file name Commented Jul 25, 2016 at 15:42
  • Teo van kot, I tried using HTTPResponseMessageand I am getting error in the best overloaded method match for has some invalid arguments in StreamConent. I am creating my first Web API Commented Jul 25, 2016 at 16:09
  • Returning serialized DataTable doesn't make sense from API, What is the end result you are hoping to get at the client side? Commented Jul 25, 2016 at 16:44
  • I want them to be JSON in response. I am converting from DataTable to the JSON using JsonConvert.SerializeObject Commented Jul 25, 2016 at 16:47

1 Answer 1

1

You can use CreateResponse method of Request object like below

    public HttpResponseMessage Get()
    {
        string fileName = "abc.json";
        return Request.CreateResponse(HttpStatusCode.OK, fileName);
    }

Edit 1:

   string contentDisposition = "inline; filename=abc.json";
   HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, byteInfo, MediaTypeHeaderValue.Parse("application/json"));
   response.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);
   return response;
Sign up to request clarification or add additional context in comments.

6 Comments

But I am converting the DataTable in to the JSON. So wont I be returning them in the String. I am sorry, I am new and little stuck.
Even if it is string you should use HttpResponseMessage to return data to client. if you need help on, how to use it on the client side let me know
'DataTable selectResults = new DataTable(); adapter.Fill(selectResults); string result = JsonConvert.SerializeObject(selectResults); HttpResponseMessage response = new HttpResponseMessage(); response.StatusCode = HttpStatusCode.OK; var fileName = "abc.json"; return Request.CreateResponse(HttpStatusCode.OK, fileName);' I tried this code and it opes the file as NewTest.JSOn with data inside as ABC.JSON. It doesnot return the original data
Oh ok I thought you only wanted to return the filename. I have modified my answer based on your above comment. Let me know if that works
Yes I tried using the above code like I showed in my comment. It opens a file as NewTest.Json(which is name of the controller of the Web API application) but if I open it inside it has data as abc.json
|

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.