C# React组件的Web Api以字符串格式返回json
在下面的代码中,我调用ASP.NET Web Api控制器来获取一些数据。问题是,C# React组件的Web Api以字符串格式返回json,c#,reactjs,asp.net-web-api,C#,Reactjs,Asp.net Web Api,在下面的代码中,我调用ASP.NET Web Api控制器来获取一些数据。问题是,response.data以字符串形式返回数组json数据(例如,“[{\“id\”:7,\“…)。我对许多其他Web Api调用使用相同的方法,它们工作得很好。这一方法的工作方式完全不同。你知道为什么会发生这种情况吗 export function FetchOverallParticipation(reviewRoundId) { var url = 'api/A/B'; return di
response.data
以字符串形式返回数组json数据(例如,“[{\“id\”:7,\“…
)。我对许多其他Web Api调用使用相同的方法,它们工作得很好。这一方法的工作方式完全不同。你知道为什么会发生这种情况吗
export function FetchOverallParticipation(reviewRoundId) {
var url = 'api/A/B';
return dispatch => {
dispatch(fetchOverallParticipationBegin());
axios.get(url, { params: { reviewRoundId } })
.then(response => {
const participationAnalytics = new schema.Entity("participationAnalytics");
const normalizedData = normalize(response.data, [participationAnalytics]);
dispatch(fetchOverallParticipationSuccess(normalizedData));
})
.catch(error => { fetchOverallParticipationFailure(error) });
}
}
下面是WebAPI方法
[HttpGet]
[Route("api/A/B")]
public IEnumerable<OverallParticipationDTO> FetchOverallParticipation(int reviewRoundId)
{
try
{
IEnumerable<OverallParticipationDTO> result =
_context.Submissions
.Where(s => s.ReviewRoundId == reviewRoundId)
.Select(s => new OverallParticipationDTO
{
Id = s.Id,
GoogleDriveDialogueFileId = s.GoogleDriveDialogueFileId,
GoogleDriveReadFileId = s.GoogleDriveReadFileId,
GoogleDriveReviseFileId = s.GoogleDriveReviseFileId,
ReviewedStudents = s.StudentGroup.GroupMemberships
.Select(gm => gm.User)
.Select(u => new ApplicationUserDto
{
Id = u.Id,
FullName = u.FullName
}),
ReviewingStudents = s.StudentGroup.AsServingReviewer.SelectMany(asr => asr.GroupReviewing.GroupMemberships)
.Select(gm => gm.User)
.Select(u => new ApplicationUserDto
{
Id = u.Id,
FullName = u.FullName
})
});
return result;
}
catch (Exception e)
{
return null;
}
}
[HttpGet]
[路线(“api/A/B”)]
公共IEnumerable FetchOverallParticipation(int reviewRoundId)
{
尝试
{
可数结果=
_背景.意见书
.其中(s=>s.ReviewRoundId==ReviewRoundId)
.选择(s=>new Overall Participation to
{
Id=s.Id,
GoogleDriveDialogueFileId=s.GoogleDriveDialogueFileId,
GoogleDriveReadFileId=s.GoogleDriveReadFileId,
GoogleDriveReviseFileId=s.GoogleDriveReviseFileId,
ReviewedStudents=s.StudentGroup.GroupMemberships
.选择(gm=>gm.User)
.选择(u=>新应用程序插入到
{
Id=u.Id,
FullName=u.FullName
}),
ReviewingStudents=s.StudentGroup.AsServingReviewer.SelectMany(asr=>asr.GroupReviewing.GroupMemberships)
.选择(gm=>gm.User)
.选择(u=>新应用程序插入到
{
Id=u.Id,
FullName=u.FullName
})
});
返回结果;
}
捕获(例外e)
{
返回null;
}
}
这与linq查询中的错误相关。of type()
不适用于转换。我应该使用is
按派生类进行筛选:
LearningActionDiscussions = s.GeneralComments
.Where(c=> c is LearningActionComment)
.OfType<LearningActionComment>()
.GroupBy(c => c.UserId)
.Select(fc => new ApplicationUserDto
{
Id = fc.Key,
FullName = fc.Select(a => a.User.FullName).ElementAt(0),
Count = fc.Count()
})
LearningActionDiscussions=s.GeneralComments
.其中(c=>c是LearningActionComment)
第()类
.GroupBy(c=>c.UserId)
。选择(fc=>新应用程序插入到
{
Id=fc.键,
FullName=fc.Select(a=>a.User.FullName).ElementAt(0),
Count=fc.Count()
})
我希望它能帮助别人。这两行代码在做什么?`const participationAnalytics=new schema.Entity(“participationAnalytics”);const normalizedata=normalize(response.data,[participationAnalytics])`