Although I’m trying to do proper error-handling in my razor pages, I would like to set up a global error page in the unlikely event of an exception, rather than displaying the ServiceStack snapshot or a blank page.
I’ve added the following in Configure.AppHost.cs, but I keep seeing the snapshot. Any idea what I’m doing wrong?
public override void Configure(Container container)
{
this.GlobalHtmlErrorHttpHandler = new RazorHandler("/oops");
I’ve also added the same error page (for now) for 404 errors, and this one is working properly (in Configure.Ui.cs:
appHost.CustomErrorHttpHandlers[HttpStatusCode.NotFound] = new RazorHandler("/oops");
Are you seeing any error related to the RazorFormat plugin? Is this added in your project? If you could provide more context about your application setup, I might have a better idea where the source of the problem might be.
One possible issue might be the RazorFormat plugin might be looking in the wrong place for your Oops.cshtml Razor page. This can be configured using the RazorFormat plugin using the PagesPath property. Eg,
If you run your application using a Debug config, you should see the specific error rather than a 'blank` snapshot HTML view. Also to confirm, what version of ServiceStack and .NET are you currently using?
No error regarding RazorFormat plugin. It is loaded, and it is handling the 404 error appropriately (I have tried specifying the PagesPath (wwwroot in my case), but it makes no difference to my problem).
I’m using ServiceStack 6.8.1 and .NET 7.0.5, though I’ve been having this particular issue for at least a year now. In Debug I am indeed seeing the snapshot HTML view.
When I explicitly throw an exception on my razor page, I get the following stack trace:
Exception Throwing an exception! System.Exception: Throwing an exception! at AspNetCore.wwwroot_app__eventId_team__teamIdentifier.ExecuteAsync() in /STRIPPED/wwwroot/app/_eventId/team/_teamIdentifier.cshtml:line 15 at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context) at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts) at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) at ServiceStack.Mvc.RazorFormat.RenderView(IRequest req, Stream stream, ViewDataDictionary viewData, IView view, String layout) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack/src/ServiceStack.Mvc/RazorFormat.cs:line 447
Not sure what other setup details I should provide to give better insight into the issue.
Could you create a minimal reproduction of the issue outside your main project and post it on GitHub? It’s hard to tell what might be happening.
It looks like you might be throwing the exception from your Razor page itself rather than a backing service/API is that right? Is that the same way you are doing the 404 exception?
If you can put together a minimal reproduction of your setup with the error/behavior you are seeing, I can better offer some alternatives.