【发布时间】:2019-02-06 00:00:51
【问题描述】:
Asp.Net Core 2.2。我正在尝试使用 Mads Kristensen 的 WebEssentials.AspNetCore.PWA 包 https://github.com/madskristensen/WebEssentials.AspNetCore.ServiceWorker 将其变成渐进式 Web 应用程序 (PWA) 的 MVC 应用程序,我在确保某些页面脱机工作时遇到了一些问题。
该应用的主要功能包括向用户提出一系列问题,并将回答保存到数据库中。此视图由 ViewModel 填充,该 ViewModel 查询数据库以显示正确的信息。
我的问题是,由于数据库内容很少更改,我是否可以缓存查询结果,以便整个应用程序可以离线工作?如果是这样,这样做的最佳做法是什么?
我的默认地图路线是控制器/动作/id,所以当尝试在 appsettings.json 中使用 routesToPreCache 时,我无法包含新创建的“事件”的 id,所以它不起作用 - 我错过了一个简单的技巧还是解决方案更复杂?
我的视图控制器如下:
public class PathwayController : Controller
{
private readonly ApplicationDbContext _context;
public PathwayController(ApplicationDbContext context)
{
_context = context;
}
public ActionResult Pathway(int? id, int? index)
{
int currentIndex = index.GetValueOrDefault();
if (currentIndex == 0)
{
ViewBag.NextIndex = 1;
}
else
{
ViewBag.NextIndex = index + 1;
}
PathwayViewModel path = new PathwayViewModel();
var incident = _context.Incident
.Include(i => i.IncidentType)
.FirstOrDefault(m => m.Id == id);
path.Incident = incident;
var stageConditions = _context.StageCondition.Where(x => x.IncidentTypeId == incident.IncidentTypeId)
.Include(s => s.Stage)
.Include(o => o.Stage.Outcome)
.OrderBy(x => x.Id)
.Skip(currentIndex)
.Take(1)
.FirstOrDefault();
path.StageCondition = stageConditions;
// Validation
var stageConditionTest = _context.StageCondition.Where(x => x.IncidentTypeId == incident.IncidentTypeId);
ViewBag.MaxIndex = stageConditionTest.Count();
//save stage to incident
incident.StageId = stageConditions.Stage.Id;
_context.Update(incident);
_context.SaveChanges();
return View(path);
}
【问题讨论】:
标签: c# asp.net-core progressive-web-apps