Search code examples
c#asp.netvisual-studiorazor-pagesasp.net5

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


I have a simple asp.net 5 razor pages app which does not show developer exception page but shows this in the browser developer tools

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.

After many iterations and debugging it turns out there was a simple typo in the sql query and instead of showing the developer error page, it was showing blank with the aforementioned error in the browser console !

Questions -

  • Is this normal/expected ?
  • any way to turn on "more" debugging to identify such errors rather than trial and error ?

environment -

  • Visual studio 2019, .net 5
  • db access using dapper v2.0.78
  • configure excerpts below !
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();
            }
              ...

there are no try/catch handlers, the code is rather basic in the razor page

        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";            
        }

this is how i figured out the error when the OnGet() would get called but 2nd line with Message = ListRows.Count would not get executed !!

in 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;
            }

        }


Solution

  • Usually in these cases, the first approach should be trying to reproduce the behaviour on a small/clean project so you can rule out various scenarios.

    As you saw, in your case it's the

     services.AddDatabaseDeveloperPageExceptionFilter()
    

    that is causing the problem. As Microsoft says in the documentation

    This should only be enabled in the Development environment.