i am creating a netcore web api and i need a List of Menus, that menus someones have a submenu list. inside a C# the submenu lists are working fine, but in the response the lists are empty objects
this is my UserService (in this class i make the call to database):
public class UserService : IUserService
{
SqlCommand sqlCommand = null;
SqlConnection sqlConnection = null;
private readonly AppSettings _appSettings;
public UserService(IOptions<AppSettings> appSettings) {
_appSettings = appSettings.Value;
}
public List<MenuResponse> GetMenu(RequestByUser model, IConfiguration Configuration) {
ClassicConnection conn = new ClassicConnection(Configuration);
SqlConnection conecction = conn.ConnectionLocal();
List<MenuResponse> lMenuResponse = new List<MenuResponse>();
try
{
Console.WriteLine("The database has been opened!");
Console.WriteLine("Connection State: " conecction.State.ToString());
sqlCommand = new SqlCommand("", conecction);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "dbo.P_GetMenu";
sqlCommand.Parameters.Add(new SqlParameter("@userName", model.userName));
sqlCommand.CommandTimeout = 15;
//sqlCommand.CommandType = CommandType.Text;
//sqlCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
MenuResponse menuResponse = new MenuResponse();
menuResponse.tmus_Username = reader.GetString(0);
menuResponse.tmap_App_ID = reader.GetString(1);
menuResponse.tmar_Role_ID = reader.GetString(2);
menuResponse.tmar_Role_Desc = reader.GetString(3);
menuResponse.tmar_Role_Ref = reader.GetString(4);
menuResponse.tmar_Role_Icon = reader.GetString(5);
menuResponse.subMenuText = reader.GetString(6);
try
{
if (reader.GetString(6) != "") {
menuResponse.subMenu = JsonConvert.DeserializeObject<List<SubMenu>>(reader.GetString(6));
}
}
catch (Exception ex){
Console.WriteLine(ex.Message);
menuResponse.subMenuText = ex.Message;
}
lMenuResponse.Add(menuResponse);
}
conecction.Close();
Console.WriteLine("The database has been closed!");
conecction.Dispose();
Console.WriteLine("The database connection has been disposed!");
Console.WriteLine("Connection State: " conecction.State.ToString());
return lMenuResponse;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
in WsController i make the response of the API:
[Route("api/[controller]")]
[ApiController]
//[Authorize]
public class WsController : ControllerBase
{
public static IWebHostEnvironment _enviroment;
private static IConfiguration Configuration;
private IUserService _userService;
public WsController(IWebHostEnvironment environment, IConfiguration _configuration, IUserService userService) {
_enviroment = environment;
Configuration = _configuration;
_userService = userService;
}
[HttpPost("GetMenu")]
[Authorize]
public IActionResult GetMenu([FromBody] RequestByUser userName) {
Response response = new Response();
List<MenuResponse> lMenuResponses = _userService.GetMenu(userName, Configuration);
if (lMenuResponses.Count == 0) {
response.done = 0;
response.msj = "Error!";
return BadRequest();
}
//var jsonString = JsonConvert.SerializeObject(lMenuResponses);
return Ok(lMenuResponses);
}
}
this is my MenuResponse class :
public class MenuResponse
{
public string tmus_Username { get; set; }
public string tmap_App_ID { get; set; }
public string tmar_Role_ID { get; set; }
public string tmar_Role_Desc { get; set; }
public string tmar_Role_Ref { get; set; }
public string tmar_Role_Icon { get; set; }
public string subMenuText { get; set; }
public List<SubMenu> subMenu { get; set; }
}
SubMenu Class:
[Serializable]
public class SubMenu
{
public string? TMRP_Privilege_ID;
public string? TMRP_TMAR_Role_ID;
public string? TMRP_Privilege_Desc;
public string? TMRP_Privilege_Ref;
public string? TMRP_Privilege_Status;
}
}
in this time am getting a object array like this (the submenu lists are empty objects but the number of registers are correct):
[
{
"tmus_Username": "epirsch",
"tmap_App_ID": "COSY",
"tmar_Role_ID": "C1",
"tmar_Role_Desc": "ORDERS INFO",
"tmar_Role_Ref": "OrdersInfo",
"tmar_Role_Icon": "list",
"subMenuText": "",
"subMenu": null
},
{
"tmus_Username": "epirsch",
"tmap_App_ID": "COSY",
"tmar_Role_ID": "C2",
"tmar_Role_Desc": "TRANSPORT",
"tmar_Role_Ref": "",
"tmar_Role_Icon": "truck",
"subMenuText": "[{\"TMRP_Privilege_ID\":\"P10\",\"TMRP_TMAR_Role_ID\":\"C2\",\"TMRP_Privilege_Desc\":\"Transport Creating\",\"TMRP_Privilege_Ref\":\"TransportCreate\",\"TMRP_Privilege_Icon\":\"plus\",\"TMRP_Privilege_Status\":\"ON\"},{\"TMRP_Privilege_ID\":\"P11\",\"TMRP_TMAR_Role_ID\":\"C2\",\"TMRP_Privilege_Desc\":\"Transport Updating\",\"TMRP_Privilege_Ref\":\"TransportUpdating\",\"TMRP_Privilege_Icon\":\"pen\",\"TMRP_Privilege_Status\":\"ON\"}]",
"subMenu": [
{},
{}
]
}
]
and the response that i want is like this:
[{
"tmus_Username": "epirsch",
"tmap_App_ID": "COSY",
"tmar_Role_ID": "C1",
"tmar_Role_Desc": "ORDERS INFO",
"tmar_Role_Ref": "OrdersInfo",
"tmar_Role_Icon": "",
"subMenuText": "",
"subMenu": null
}, {
"tmus_Username": "epirsch",
"tmap_App_ID": "COSY",
"tmar_Role_ID": "C2",
"tmar_Role_Desc": "TRANSPORT",
"tmar_Role_Ref": "",
"tmar_Role_Icon": "",
"subMenuText": "[{\"TMRP_Privilege_ID\":\"P10\",\"TMRP_TMAR_Role_ID\":\"C2\",\"TMRP_Privilege_Desc\":\"Transport Creating\",\"TMRP_Privilege_Ref\":\"TransportCreate\",\"TMRP_Privilege_Status\":\"ON\"},{\"TMRP_Privilege_ID\":\"P11\",\"TMRP_TMAR_Role_ID\":\"C2\",\"TMRP_Privilege_Desc\":\"Transport Updating\",\"TMRP_Privilege_Ref\":\"TransportUpdating\",\"TMRP_Privilege_Status\":\"ON\"}]",
"subMenu": [{
"TMRP_Privilege_ID": "P10",
"TMRP_TMAR_Role_ID": "C2",
"TMRP_Privilege_Desc": "Transport Creating",
"TMRP_Privilege_Ref": "TransportCreate",
"TMRP_Privilege_Status": "ON"
}, {
"TMRP_Privilege_ID": "P11",
"TMRP_TMAR_Role_ID": "C2",
"TMRP_Privilege_Desc": "Transport Updating",
"TMRP_Privilege_Ref": "TransportUpdating",
"TMRP_Privilege_Status": "ON"
}]
}]
CodePudding user response:
you will have to fix your SubMenu class by adding {get;set;}
public class SubMenu
{
public string? TMRP_Privilege_ID { get; set; }
public string? TMRP_TMAR_Role_ID { get; set; }
public string? TMRP_Privilege_Desc { get; set; }
public string? TMRP_Privilege_Ref { get; set; }
public string? TMRP_Privilege_Status { get; set; }
}
and IMHO you don't need try..catch here, use this code
var subMenuText = reader.GetString(6);
menuResponse.subMenuText = subMenuText; // do you really need it?
if (!string.IsNullOrEmpty(subMenuText))
menuResponse.subMenu = JsonConvert.DeserializeObject<List<SubMenu>>(subMenuText);