我正在创建此MVC应用程序,并且下面的代码相同,用于另一种方法的命名/措辞也不同。但是,此方法始终会引发此错误:
System.ArgumentNullException: Value cannot be null.
Parameter name: source
我到处搜索过,但没有运气来解决这个问题:
我的控制器中的代码是:
public List<Quote> GetQuote(string symbol1) //this action method returns the quote API endpoint
{
// string to specify information to be retrieved from the API
string IEXTrading_API_PATH = BASE_URL + "stock/" + symbol1 + "/quote";
// initialize objects needed to gather data
string CompanyQuote = "";
List<Quote> DailyQuote = new List<Quote>();
httpClient.BaseAddress = new Uri(IEXTrading_API_PATH);
// connect to the API and obtain the response
HttpResponseMessage response = httpClient.GetAsync(IEXTrading_API_PATH).GetAwaiter().GetResult();
// now, obtain the Json objects in the response as a string
if (response.IsSuccessStatusCode)
{
CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}
// parse the string into appropriate objects
if (!CompanyQuote.Equals(""))
{
QuoteRoot quoteroot = JsonConvert.DeserializeObject<QuoteRoot>(CompanyQuote,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
DailyQuote = quoteroot.quote.ToList();
}
// fix the relations. By default the quotes do not have the company symbol
// this symbol serves as the foreign key in the database and connects the quote to the company
foreach (Quote quotee in DailyQuote)
{
quotee.companysymbol = symbol1;
}
return DailyQuote;
}
返回此错误的行在此块上:
// parse the string into appropriate objects
if (!CompanyQuote.Equals(""))
{
QuoteRoot quoteroot = JsonConvert.DeserializeObject<QuoteRoot>(CompanyQuote,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
DailyQuote = quoteroot.quote.ToList();
}
答案 0 :(得分:0)
我猜以下部分会抛出异常:
if (response.IsSuccessStatusCode)
{
CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}
请检查内容是否为空:
if (response.IsSuccessStatusCode && response.Content != null)
{
CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}