在這裏搜索了兩個多小時之後,我仍然無法從我這裏拼湊起來的代碼,因爲我相信我缺少一個參考。使用System.Web.Http的引用不起作用? (使用.net 4.5)
我得到的錯誤來自下面的部分,其中請求在當前上下文中不存在。
return Request.CreateResponse(HttpStatusCode.BadRequest);
我得到的另一個錯誤是getFileFromID在當前上下文中也不存在。
getFileFromID(id, out fileName, out fileSize);
我敢肯定,這只是一個簡單的參考,我失蹤,但我試着用搜索引擎,但還是無法找到解決方案。有誰知道我爲什麼不斷收到「在當前上下文中不存在」的錯誤?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Morningstar.JSON;
using Ionic.Zip;
using System.Net;
using System.IO.Compression;
using System.Windows.Forms;
using Ionic.Zip;
using System.Web;
using System.Net.Http;
using System.Web.Http;
namespace MS_Hourly_API_Call
{
class Program
{
public static void Main(string[] args)
{
// THIS ONE IS DIFFERENT FROM THE ORIGINAL DOWNLOADCURVEDATA
string feedName = "RiskReporting_Power_Hub_Hourly";
var url = String.Format("https://mp.morningstarcommodity.com/lds/lists/{0}/content?fromDateTime={1}", feedName, DateTime.Today.ToString("yyyy-MM-dd"));
string username = "asdfk"; //removed username
string password = "asdfasdf"; //removed password
// Setup web connection with appropriate authentication parameters and requesting a JSON response
using (var syncClient = new WebClient())
{
syncClient.Headers.Add("Accept", "application/json");
syncClient.Credentials = new NetworkCredential(username, password);
// Retrieve and parse the JSON response
var jsonContent = syncClient.DownloadString(url);
var feedItems = JsonConvert.DeserializeObject<List<FeedContent>>(jsonContent);
// Download each item
syncClient.Headers.Remove("Accept");
}
}
public HttpResponseMessage GetFile(string id)
{
if (String.IsNullOrEmpty(id))
return Request.CreateResponse(HttpStatusCode.BadRequest);
string fileName;
string localFilePath;
int fileSize;
localFilePath = getFileFromID(id, out fileName, out fileSize);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
return response;
}
}
}
那麼牛逼你是否期望*'要求'是?我的猜測是你實際上試圖調用[此方法](https://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessageextensions.createresponse(v = vs.118).aspx )但你錯過了一個事實,即在最初看到代碼的上下文中,有一個'Request'屬性... –
這個代碼試圖做的事情一點都不清楚,實際上......爲什麼如果您只是不提供請求的控制檯應用程序,您是否嘗試創建響應? –
它看起來像你的項目是一個控制檯應用程序。 Request屬性可以從Web項目類型訪問。 – Borophyll