我正在設置由ASP .NET WEB API返回的文件的名稱。目前,它只是以URL中傳遞參數的名義返回。但要返回,如果我需要什麼,然後作爲ABC.JSON設置由ASP .NET Web API返回的文件名
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);
}}}
我沒有看到在使用內容處置其他論壇,但我不是在代碼中使用類HTTPResponse。如何才能做到這一點。由於
我試着像下面
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);
但它引發的錯誤StreamContent說,最好的重載的方法匹配具有StreamConent一些無效參數
從控制器返回'string'。您應該至少使用'IHttpActionResult',但更好的'HttpResponseMessage'來設置您的文件名 –
Teo van kot,我嘗試使用HTTPResponseMessage和我在最佳重載方法匹配中獲取錯誤在StreamConent中有一些無效參數。我正在創建我的第一個Web API – trx
從API返回序列化的DataTable沒有意義,您希望在客戶端獲得的最終結果是什麼? – Win