首页 > 解决方案 > Razor Pages - 动态添加元刷新标签

问题描述

使用 DotNetCore 2.2。

我想动态地向我的 Razor 页面添加元刷新。

<head>
  ...
  <meta http-equiv="refresh" content="30">
</head>

IE

public async Task OnGet()
{
    if (something) {
        // add the meta tag
    }
}       

我的 _Layout 目前是新项目的默认布局。

标签: razor-pages

解决方案


也许有一种更优雅的方式来做到这一点。

但我去了——

public async Task OnGet()
{
    if (something) {
        // add the meta tag
        ViewData["ShowMeta"] = _refreshSeconds.ToString();
    }
}   

在 _Layout.cshtml

@if (ViewData["ShowMeta"] != null)
{
    <meta http-equiv="refresh" content="@ViewData["ShowMeta"]">
}

推荐阅读