【发布时间】:2020-05-06 23:30:42
【问题描述】:
有没有办法在页面上全局设置 ViewData 键? 在我的示例中,我必须在每个动词上设置它。
在official documentation 或this probably unofficial 但更好 文档上我没有找到任何关于此问题的信息。
[AllowAnonymous]
public class PasswordResetModel : PageModel {
private readonly IConfiguration _configuration;
private readonly ILogger<PasswordResetModel> _logger;
private readonly IRecaptchaService _recaptchaService;
private readonly RecaptchaOptions _recaptchaOptions;
public PasswordResetModel(IConfiguration configuration,
ILogger<PasswordResetModel> logger,
IRecaptchaService recaptchaService) {
_configuration = configuration;
_logger = logger;
_recaptchaService = recaptchaService;
_recaptchaOptions = new RecaptchaOptions();
_configuration.Bind("reCAPTCHA", _recaptchaOptions);
//ViewData["ping"] = "pong"; -- here is not possible
}
[BindProperty]
public PasswordResetRequest passwordResetRequest { get; set; }
public void OnGet() {
ViewData["IsRecaptcaEnabledOnPasswordReset"] = _recaptchaOptions.IsRecaptcaEnabledOnPasswordReset;
}
public async Task<IActionResult> OnPostAsync() {
ViewData["IsRecaptcaEnabledOnPasswordReset"] = _recaptchaOptions.IsRecaptcaEnabledOnPasswordReset;
//code skipped intentionally
}
}
谢谢,
【问题讨论】:
标签: c# .net-core razor-pages