c# - Setting the file name returned by ASP .NET Web API -
i looking set name of file being returned asp .net web api. returns in name of parameters being passed in url. if need returned 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 * stpr_study 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 not using httpresponse in code. how can done. thanks
i tried 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 throws error in streamcontent saying,the best overloaded method match has invalid arguments in streamconent
you can use createresponse method of request object 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;
Comments
Post a Comment