我正在创建这个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();
}
发布于 2021-02-19 12:33:44
我猜下面的部分会抛出异常:
if (response.IsSuccessStatusCode)
{
CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}
请检查内容是否为空:
if (response.IsSuccessStatusCode && response.Content != null)
{
CompanyQuote = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}
https://stackoverflow.com/questions/55566869
复制相似问题