【发布时间】:2020-04-29 17:54:48
【问题描述】:
我在 ASP.NET 核心中使用 Razor Pages 作为学习练习,当我尝试在 viewData 中使用未定义的键时,我注意到的一件事是没有引发异常。我假设这是有意的,但我想知道......为什么?我应该如何检测此类错误?似乎很容易打错字,而且在为时已晚之前很难注意到。一个例子:
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace Infodawn.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
// doesn't throw exception, even though viewData["bar"] is undefined:
ViewData["foo"] = ViewData["bar"];
// throws exception, as expected:
Dictionary<string, int> dict = new Dictionary<string, int>();
int a = dict["bar"];
}
}
}
【问题讨论】:
标签: asp.net razor razor-pages