C
Carneno
Guest
Hello,
I have an C#, Asp.Net, Core 2.0, MVC application developed in Visual Studio 2017.
When I execute this code:
public async Task<List<Product>> GetProductListAPIAsync(string serverUrl, string serverKey, string queryCall, List<Category> categories, CancellationToken cancelToken = default(CancellationToken))
{
var uri = Util.processServiceUri(serverUrl, queryCall);
KeyAndParametersProductList keyAndParameters = new KeyAndParametersProductList() { key = serverKey };
foreach (Category cat in categories)
{
foreach (SubCategory subcat in cat.subcategories)
{
keyAndParameters.categories.Add(subcat.name);
}
}
var json = JsonConvert.SerializeObject(keyAndParameters);
var response = await Request(HttpMethod.Post, uri, json, new Dictionary<string, string>());
string responseText = await response.Content.ReadAsStringAsync();
var serializedResult = JsonConvert.DeserializeObject<Products>(responseText);
return (serializedResult.products);
}
It gives the error "NullReferenceException: Object reference not set to an instance of an object." on this line: keyAndParameters.categories.Add(subcat.name);
Here are the Models for the code:
namespace ChinavasionAPI.Models
{
public class Categories
{
public List<Category> categories { get; set; }
}
public class Category
{
[Display(Name = "Category Name")]
public string name { get; set; }
public string url { get; set; }
public string image { get; set; }
public string start_content { get; set; }
public string finish_content { get; set; }
public List<SubCategory> subcategories { get; set; }
}
public class SubCategory
{
public string name { get; set; }
public string url { get; set; }
public string image { get; set; }
public string start_content { get; set; }
public string finish_content { get; set; }
}
}
using System.Collections.Generic;
namespace ChinavasionAPI.Models
{
public class KeyAndParametersProductList
{
public string key { get; set; }
public List<string> categories { get; set; }
}
}
I'm want to populate the KeyAndParametersProductList.categories from the public List<SubCategory> subcategories { get; set; }.
I can't figure out how to code it to work.
Thanks,
Tony
Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.
Continue reading...
I have an C#, Asp.Net, Core 2.0, MVC application developed in Visual Studio 2017.
When I execute this code:
public async Task<List<Product>> GetProductListAPIAsync(string serverUrl, string serverKey, string queryCall, List<Category> categories, CancellationToken cancelToken = default(CancellationToken))
{
var uri = Util.processServiceUri(serverUrl, queryCall);
KeyAndParametersProductList keyAndParameters = new KeyAndParametersProductList() { key = serverKey };
foreach (Category cat in categories)
{
foreach (SubCategory subcat in cat.subcategories)
{
keyAndParameters.categories.Add(subcat.name);
}
}
var json = JsonConvert.SerializeObject(keyAndParameters);
var response = await Request(HttpMethod.Post, uri, json, new Dictionary<string, string>());
string responseText = await response.Content.ReadAsStringAsync();
var serializedResult = JsonConvert.DeserializeObject<Products>(responseText);
return (serializedResult.products);
}
It gives the error "NullReferenceException: Object reference not set to an instance of an object." on this line: keyAndParameters.categories.Add(subcat.name);
Here are the Models for the code:
namespace ChinavasionAPI.Models
{
public class Categories
{
public List<Category> categories { get; set; }
}
public class Category
{
[Display(Name = "Category Name")]
public string name { get; set; }
public string url { get; set; }
public string image { get; set; }
public string start_content { get; set; }
public string finish_content { get; set; }
public List<SubCategory> subcategories { get; set; }
}
public class SubCategory
{
public string name { get; set; }
public string url { get; set; }
public string image { get; set; }
public string start_content { get; set; }
public string finish_content { get; set; }
}
}
using System.Collections.Generic;
namespace ChinavasionAPI.Models
{
public class KeyAndParametersProductList
{
public string key { get; set; }
public List<string> categories { get; set; }
}
}
I'm want to populate the KeyAndParametersProductList.categories from the public List<SubCategory> subcategories { get; set; }.
I can't figure out how to code it to work.
Thanks,
Tony
Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.
Continue reading...