我创建了一个从角度前端调用的web api。当我调试我的Post调用时,它按预期工作,但是当我返回我的' newItem'它继续在网络XHR日志中说待处理。如果我将OK(newItem)更改为204状态代码,它确实可以正常工作,并且它不会保持挂起状态。任何人有任何想法可能导致这种行为?提前谢谢了。
我的API代码
using DWP.Services.Interfaces;
using DWP.ViewModels;
using System.Web.Http;
namespace DWP.Api
{
[Authorize()]
[RoutePrefix("api/teamnewsviews")]
public class TeamnewsViewsController : ApiController
{
ITeamnewsViewsService _teamnewsViewsService;
public TeamnewsViewsController(ITeamnewsViewsService teamnewsViewsService)
{
this._teamnewsViewsService = teamnewsViewsService;
}
[HttpGet]
// GET api/<controller>
public IHttpActionResult Get(string teamnewsItemId)
{
TeamnewsViewsViewModel newsItem = new TeamnewsViewsViewModel();
var teamNewsItem = _teamnewsViewsService.GetTeamnewsView(teamnewsItemId);
newsItem.Id = teamNewsItem.Id;
newsItem.TenantId = teamNewsItem.TenantId;
newsItem.NewsItemId = teamNewsItem.NewsItemId;
newsItem.ViewCount = teamNewsItem.ViewCount;
return Ok(newsItem);
}
[HttpPost]
[ActionName("Increment")]
public IHttpActionResult Post(string teamnewsItemId)
{
if (teamnewsItemId == "")
{
return BadRequest("The newsitem Id is invalid");
}
var teamNewsItem = _teamnewsViewsService.GetTeamnewsView(teamnewsItemId);
if (teamNewsItem == null)
{
return NotFound();
}
var newItem = _teamnewsViewsService.IncrementViewCount(teamNewsItem);
// var updatedTeamnewsitem = _teamnewsViewsService.GetTeamnewsView(teamnewsItemId);
return Ok(newItem);
}
}
}
我的数据库服务实现:
using DWP.Data.Helpers.Interfaces;
using DWP.Data.Model;
using DWP.Services.Interfaces;
using System.Linq;
namespace DWP.Services
{
public class TeamnewsViewsService : ITeamnewsViewsService
{
private IUnitOfWork _unitOfWork;
public TeamnewsViewsService(IUnitOfWork uow)
{
this._unitOfWork = uow;
}
public TeamnewsView GetTeamnewsView(string teamnewsId)
{
TeamnewsView teamnewsViews =
this._unitOfWork
.GetRepository<TeamnewsView>()
.GetAll()
.Where(tnv => tnv.NewsItemId == teamnewsId).FirstOrDefault();
return teamnewsViews;
}
public TeamnewsView IncrementViewCount(TeamnewsView teamnewsView)
{
teamnewsView.ViewCount = teamnewsView.ViewCount + 1;
this._unitOfWork.Commit();
var newTeamNewsItem = GetTeamnewsView(teamnewsView.NewsItemId);
return newTeamNewsItem;
}
}
}
答案 0 :(得分:0)
事实证明它对另一个表有一个cericular引用错误。我通过制作一个视图模型来修复它,如下面的代码所示。
TeamnewsViewsViewModel vm = new TeamnewsViewsViewModel();
var newItem = _teamnewsViewsService.IncrementViewCount(teamNewsItem);
vm.ViewCount = newItem.ViewCount;