根据条件显示主页
Display Home page based on a condition
我有一个 ASP.Net MVC 应用程序,其中 return 有一个名为 Customer 的页面作为主页。但是当用户试图在假期访问应用程序时(假期列表存储在 table 中)而不是客户页面,我需要显示一个应用程序不可用页面。在启动时我有如下所示
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Customers}/{action=Index}/{id?}");
});
现在我创建了一个名为 AppNotAvialable.cshmtl 的新页面并尝试 return 如果今天在 HolidayWeeks 中被存储为假期 table
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
var timeUtc = DateTime.UtcNow;
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var todayDt = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);
var holidaycheck = (from hc in _context.HolidayWeeks
where hc.HolidateDate.Date == todayDt.Date
select hc).Count();
if (holidaycheck != 0)
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
else
{
// Show customer page
我担心的是,每次用户尝试访问主页时,都会对数据库进行查询以检查今天是否是假期。所以我想检查是否有更好的检查方法,我是否应该将数据存储在 session/cache 中,以便每次他们访问主页时都不会进行查询。谁能建议我该怎么做
如果你的假期有固定的日子,比如周六和周日,你可以这样做。这里只是写了一个没有缓存的demo,大家可以参考一下。
型号:
public static class CacheKeys
{
public static string Saturday => "Saturday";
public static string Sunday => "Sunday";
}
控制器:
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
DayOfWeek wk = DateTime.Today.DayOfWeek;
if (CacheKeys.Saturday == wk.ToString()|| CacheKeys.Sunday == wk.ToString())
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
else
{
return View("/Views/Customers/WorkDay.cshtml");
}
}
只需判断当前星期几,结合你固定的放假时间即可。
当然可以把数据存入session/cache,你可以参考文档添加。
https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-6.0
将MemoryCache
服务添加到服务集合中(该服务将数据缓存在服务器上):
services.AddMemoryCache();
然后可以通过接口DI注入IMemoryCache
.
//Check if there is a cached holiday vm
if (!memoryCache.TryGetValue("HolidayModelVM", out HolidayModelVM HM))//"HolidayModelVM" is the cache key for the vm
{
//Not present in the cache, we create a new one using the database
HM = new HolidayModelVM
{
//Fetch the actual entity that contains the holiday dates
}
//Cache the VM
_ = memoryCache.Set<HolidayModelVM>("HolidayModelVM", HM, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(4),//Expiration time, when expired, the database will be used to fetch and cache a new version of the data.
SlidingExpiration = TimeSpan.FromHours(2)
});
}
//Do your holiday check using the HolidayModelVM
foreach (DayOfWeek offDay in HM.Holidays) //Holidays is a collection of DayOfWeek enums
{
if (DateTime.UtcNow.DayOfWeek == offDay)
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
}
return View("CustomerPage");
更新假期数据时,从缓存中清除 VM,以便下次有人访问该页面时获取新数据:
memoryCache.Remove("HolidayModelVM");
您也可以像@jeremy-lakeman 建议的那样将所有这些包装在一个单例服务中,在那里执行所有缓存逻辑,然后注入并获取是否是假期,并 return 相应地得到结果。
我有一个 ASP.Net MVC 应用程序,其中 return 有一个名为 Customer 的页面作为主页。但是当用户试图在假期访问应用程序时(假期列表存储在 table 中)而不是客户页面,我需要显示一个应用程序不可用页面。在启动时我有如下所示
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Customers}/{action=Index}/{id?}");
});
现在我创建了一个名为 AppNotAvialable.cshmtl 的新页面并尝试 return 如果今天在 HolidayWeeks 中被存储为假期 table
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
var timeUtc = DateTime.UtcNow;
var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var todayDt = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);
var holidaycheck = (from hc in _context.HolidayWeeks
where hc.HolidateDate.Date == todayDt.Date
select hc).Count();
if (holidaycheck != 0)
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
else
{
// Show customer page
我担心的是,每次用户尝试访问主页时,都会对数据库进行查询以检查今天是否是假期。所以我想检查是否有更好的检查方法,我是否应该将数据存储在 session/cache 中,以便每次他们访问主页时都不会进行查询。谁能建议我该怎么做
如果你的假期有固定的日子,比如周六和周日,你可以这样做。这里只是写了一个没有缓存的demo,大家可以参考一下。
型号:
public static class CacheKeys
{
public static string Saturday => "Saturday";
public static string Sunday => "Sunday";
}
控制器:
public async Task<IActionResult> Index(string sortOrder, string searchString,
int? pageNumber, string currentFilter)
{
DayOfWeek wk = DateTime.Today.DayOfWeek;
if (CacheKeys.Saturday == wk.ToString()|| CacheKeys.Sunday == wk.ToString())
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
else
{
return View("/Views/Customers/WorkDay.cshtml");
}
}
只需判断当前星期几,结合你固定的放假时间即可。
当然可以把数据存入session/cache,你可以参考文档添加。
https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-6.0
将MemoryCache
服务添加到服务集合中(该服务将数据缓存在服务器上):
services.AddMemoryCache();
然后可以通过接口DI注入IMemoryCache
.
//Check if there is a cached holiday vm
if (!memoryCache.TryGetValue("HolidayModelVM", out HolidayModelVM HM))//"HolidayModelVM" is the cache key for the vm
{
//Not present in the cache, we create a new one using the database
HM = new HolidayModelVM
{
//Fetch the actual entity that contains the holiday dates
}
//Cache the VM
_ = memoryCache.Set<HolidayModelVM>("HolidayModelVM", HM, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(4),//Expiration time, when expired, the database will be used to fetch and cache a new version of the data.
SlidingExpiration = TimeSpan.FromHours(2)
});
}
//Do your holiday check using the HolidayModelVM
foreach (DayOfWeek offDay in HM.Holidays) //Holidays is a collection of DayOfWeek enums
{
if (DateTime.UtcNow.DayOfWeek == offDay)
{
return View("/Views/Customers/AppNotAvailable.cshtml");
}
}
return View("CustomerPage");
更新假期数据时,从缓存中清除 VM,以便下次有人访问该页面时获取新数据:
memoryCache.Remove("HolidayModelVM");
您也可以像@jeremy-lakeman 建议的那样将所有这些包装在一个单例服务中,在那里执行所有缓存逻辑,然后注入并获取是否是假期,并 return 相应地得到结果。