为什么 asp.net 5 个剃须刀页面中没有开发者例外页面?

Why no developer exception page in asp.net 5 razor pages?

我有一个简单的 asp.net 5 razor pages 应用程序,它不显示开发人员异常页面,但在浏览器开发人员工具中显示此内容

The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.

经过多次迭代和调试后发现 sql 查询中存在一个简单的拼写错误,它没有显示开发人员错误页面,而是在浏览器控制台中显示上述错误的空白!

问题 -

环境-

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logg)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseDeveloperExceptionPage();
                //app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
              ...

没有 try/catch 处理程序,代码相当基础 在剃须刀页面

        public IEnumerable<gEmployee> ListRows { get; private set; }
        DAL mDAL;
        public string Message;

        public void OnGet(int dID)
        {
            ListRows = mDAL.GetEmployees(dID);

            Message = $"Got {ListRows.Count()} Rows";            
        }

这就是我在 OnGet() 被调用时发现错误的方法,但是带有 Message = ListRows.Count 的第二行不会被执行!!

在 GetEmployees

        public List<gEmployee> GetEmployees(int dID)
        {
            using (var conn = new SqlConnection(cx.DefaultConnection))
            {
                var sql = @"SELECT * from gEmployee ";
                if (dID > 0)
                    sql += " WHERE dID = @dID ";

                var ListRows = conn.Query<gEmployee>(sql, new { dID = dID}).ToList();
                return ListRows;
            }

        }

如果您的服务器端代码出现异常,开发者错误页面将显示。根据控制台消息,您的 SQL 拼写错误很可能没有导致引发异常。或者,如果确实如此,您可能将其隐藏在 try - catch 块中。您实际上没有显示相关代码,所以这纯粹是猜测。无论哪种方式,都会导致您的视图页面尝试呈现浏览器无法理解的内容,因此浏览器会在控制台中通知您。

如果您正在编写 SQL,在将其交给您的代码执行之前,在 SQL Server Management Studio 中对其进行测试总是很有用的。

Is this normal/expected

您正在使用用于 Web 开发的高级框架 (.net),因此,调试不像其他平台(如 PHP 框架)那么简单) 除非你足够了解项目的依赖关系和底层 details/behaviors。这样,您有时可能会看到 none 相关或不明确的错误。在我看来,使用高级框架需要更高水平的知识和经验。在您的情况下,主要仔细检查要故意使用的方法和属性,因为它们可以 disable/disturb 您所说的 而不是显示的内置异常处理程序行为开发人员错误页面,它在浏览器控制台中显示为空白并出现上述错误

any way to turn on "more" debugging to identify such errors rather than trial and error ?

您可以编写一个 HandleError 方法,其中包含 Handle errors in ASP.NET Core 中可用的一种方法,以便在您需要的地方使用它,如下所示:

private void HandleError()
{
    ...
    var exceptionHandlerPathFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
    ....
}

public void OnGet(string code)
{
    //do stuff
    HandleError();
    //return
}

请阅读整个 MS Doc 页面,以找出最适合您情况的文档来解决主要问题。问题解决后,就不用再用上面的技巧了

通常在这些情况下,第一种方法应该是尝试在 small/clean 项目上重现行为,这样您就可以排除各种情况。

如您所见,在您的情况下是

 services.AddDatabaseDeveloperPageExceptionFilter()

这是导致问题的原因。 作为Microsoft says in the documentation

This should only be enabled in the Development environment.