You can create a custom class matching this structure and return an instance of that class or a list of that class for the controller method.
how to get the response in the same way for which i am sharing a sample
This is the response sample
releaseversiontypeview = [
{
version: 'v5.1.1',
adjuster: {
Improvement: [{
data: 'Adjusters no longer have the capability to update address on Profile. Must contact Manager or Resource Team staff for any updates. '
}],
newFeature: []
},
staff: {
Improvement: [{
data: 'Adjusters no longer have the capability to update address on Profile. Must contact Manager or Resource Team staff for any updates. '
}],
newFeature: []
},
client: null
},
DROP TABLE IF Exists [dbo].[ReleaseNote] GO CREATE TABLE [DBO].ReleaseNote ( ReleaseNoteID INT IDENTITY(1, 1) Not Null, ReleaseNoteDescription NVARCHAR(500) Not Null, CreatedBy INT Null, CreatedDate DATETIME Null, ModifiedBy Int Null, ModifiedDate DateTime Null, ReleaseVersionID INT FOREIGN KEY REFERENCES dbo.ReleaseVersion(ReleaseVersionID) Null, ReleaseItemTypeID INT FOREIGN KEY (ReleaseItemTypeID) REFERENCES ReleaseItemType(ReleaseItemTypeID) Null, UserTypeID INT FOREIGN KEY (UserTypeID) REFERENCES UserType(UserTypeID), PRIMARY KEY CLUSTERED ( [ReleaseNoteID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO -------------------------------------------------------------
this is the api
[Route("api/ReleaseNote")]
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public async Task<IHttpActionResult> GetReleaseNotes() #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{ ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal; Int32 LogedInID = Convert.ToInt32(principal.Claims.FirstOrDefault(t => t.Type == "LogedInID").Value); ReleaseNoteViewModel Releasenote = new ReleaseNoteViewModel();
try {
Releasenote.ReleaseVersionTypeView = db.ReleaseVersions.AsNoTracking().OrderBy(x => x.ReleaseVersionName).Select(x => new ReleaseVersionModel { ReleaseVersionID = x.ReleaseVersionID,
ReleaseVersionName = x.ReleaseVersionName,
ReleaseUserTypeView = db.UserTypes.AsNoTracking().OrderBy(ka => ka.UserTypeName).Select(ka => new UserTypeModel
{
UserTypeID = ka.UserTypeID,
UserTypeName = ka.UserTypeName,
CreatedBy = ka.CreatedBy,
CreatedDate = ka.CreatedDate,
ModifiedBy = ka.ModifiedBy,
ModifiedDate = ka.ModifiedDate,
ReleaseItemTypeView = db.ReleaseItemTypes.OrderBy(t => t.ReleaseItemTypeName).Select(t => new ReleaseItemModel
{
ReleaseItemTypeID = t.ReleaseItemTypeID,
ReleaseItemTypeNote = t.ReleaseItemTypeName,
ReleaseNoteTypeView = db.ReleaseNotes.OrderBy(a=> a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel
{
ReleaseNoteID = a.ReleaseNoteID,
ReleaseNoteDescription = a.ReleaseNoteDescription, }).ToList<ReleaseNotesModel>()
}).ToList<ReleaseItemModel>()
}).ToList<UserTypeModel>()
}).ToList<ReleaseVersionModel>();
}
catch (Exception ex)
{
Global.InsertException(ex);
}
return Ok(Releasenote);
this is the view model
using System;
using System.Collections.Generic;
namespace WebApplication1.Models {
// Models returned by AccountController actions.
public class ReleaseNotesModel
{
public int ReleaseNoteID { get; set; }
public string ReleaseNoteDescription { get; set; }
public int? CreatedBy { get; set; }
public DateTime? CreatedDate { get; set; }
public int? ModifiedBy { get; set; }
public DateTime? ModifiedDate { get; set; }
public int? ReleaseVersionID { get; set; }
public int? ReleaseItemTypeID { get; set; }
public int? UserTypeID { get; set; } }
public class ReleaseItemModel { public int ReleaseItemTypeID { get; set; } public string ReleaseItemTypeNote { get; set; } public int? CreatedBy { get; set; } public DateTime? CreatedDate { get; set; } public int? ModifiedBy { get; set; } public DateTime? ModifiedDate { get; set; } public List<ReleaseNotesModel> ReleaseNoteTypeView { get; set; } } public class UserTypeModel { public int UserTypeID { get; set; } public string UserTypeName { get; set; } public int? CreatedBy { get; set; } public DateTime? CreatedDate { get; set; } public int? ModifiedBy { get; set; } public DateTime? ModifiedDate { get; set; } public List<ReleaseItemModel> ReleaseItemTypeView { get; set; } } public class ReleaseVersionModel { public int ReleaseVersionID { get; set; } public string ReleaseVersionName { get; set; } public bool IsCurrentVersion { get; set; } public int? CreatedBy { get; set; } public DateTime? CreatedDate { get; set; } public int? ModifiedBy { get; set; } public DateTime? ModifiedDate { get; set; } public List<UserTypeModel> ReleaseUserTypeView { get; set; } }
public class ReleaseNoteViewModel {
public List<ReleaseNotesModel> ReleaseNoteTypeView { get; set; }
public List<ReleaseItemModel> ReleaseItemTypeView { get; set; }
public List<UserTypeModel> ReleaseUserTypeView { get; set; }
public List<ReleaseVersionModel> ReleaseVersionTypeView { get; set; }
} }
and i am getting the response :
{ "ReleaseNoteTypeView": null, "ReleaseItemTypeView": null, "ReleaseUserTypeView": null, "ReleaseVersionTypeView": null }
don't know where is the mistake
2 answers
Sort by: Most helpful
-
-
Lan Huang-MSFT 29,166 Reputation points Microsoft Vendor
2023-09-11T05:50:55.7833333+00:00 Hi @Basit Nisar,
You can run the Web API project and search
https://localhost:*****/api/ReleaseNote
from your browser. Use the following code to view specific error messages in your browser.catch ( Exception ex) { throw(ex); }
First, there is no ReleaseItemTypeName parameter in ReleaseItemTypeView.
Remove
AsNoTracking()
method.This is all the code:
[Route("api/ReleaseNote")] #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public async Task<IHttpActionResult> GetReleaseNotes() #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { // ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal; //Int32 LogedInID = Convert.ToInt32(principal.Claims.FirstOrDefault(t => t.Type == "LogedInID").Value); ReleaseNoteViewModel Releasenote = new ReleaseNoteViewModel(); try { Releasenote.ReleaseVersionTypeView = db.ReleaseVersions.OrderBy(x => x.ReleaseVersionName).Select(x => new ReleaseVersionModel { ReleaseVersionID = x.ReleaseVersionID, ReleaseVersionName = x.ReleaseVersionName, ReleaseUserTypeView = db.UserTypes.OrderBy(ka => ka.UserTypeName).Select(ka => new UserTypeModel { UserTypeID = ka.UserTypeID, UserTypeName = ka.UserTypeName, CreatedBy = ka.CreatedBy, CreatedDate = ka.CreatedDate, ModifiedBy = ka.ModifiedBy, ModifiedDate = ka.ModifiedDate, ReleaseItemTypeView = db.ReleaseItemTypes.OrderBy(t => t.ReleaseItemTypeNote).Select(t => new ReleaseItemModel { ReleaseItemTypeID = t.ReleaseItemTypeID, ReleaseItemTypeNote = t.ReleaseItemTypeNote, ReleaseNoteTypeView = db.ReleaseNotes.OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteDescription = a.ReleaseNoteDescription, }).ToList<ReleaseNotesModel>() }).ToList<ReleaseItemModel>() }).ToList<UserTypeModel>() }).ToList<ReleaseVersionModel>(); } catch (Exception ex) { throw(ex); } return Ok(Releasenote); }
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.