rossipedia
rossipedia

Reputation: 59397

ServiceStack.Razor CustomHttpHandler not displaying for HttpStatusCode.BadRequest

I'm trying to get a minimal app working using ServiceStack.Razor, and I'm having trouble getting a CustomHttpHandler to work. I've followed the instructions here and here, but it's not working right.

I'm using the following code to register a custom http handler for HttpStatusCode.BadRequest:

public override void Configure(Container container)
{
    this.Plugins.Add(new RazorFormat());
    this.SetConfig(new EndpointHostConfig
    {
        CustomHttpHandlers =
        {
            { HttpStatusCode.NotFound, new RazorHandler("/notfound") }, 
            { HttpStatusCode.BadRequest, new RazorHandler("/error") }
        }, 
        DebugMode = true
     });
}

The thing is, the /notfound handler works perfectly for 404s, but no matter what I do, I can't get the /error razor file to display whenever an ArgumentNullException is thrown.

My service method looks like this:

public object Any(Hello request)
{
    if (string.IsNullOrEmpty(request.Name))
    {
        throw new ArgumentNullException("Name");
    }

    return new HelloResponse { Result = "Hello " + request.Name };
}

ServiceStack returns a 400 status, which is fine, but it still displays the view I have for HelloResponse:

400-bad-request

What am I missing? Am I misunderstanding how CustomHttpHandlers are supposed to work?

For reference, I put the project up on github.

Upvotes: 3

Views: 580

Answers (1)

mythz
mythz

Reputation: 143339

Yeah the CustomHttpHandlers are just meant for handling un-handled system generated errors. Currently they're limited to:

  • NotFound (404) for un-handled requests
  • Forbidden (403) when a request is made to an forbidden file or resource

These are the errors happen outside of ServiceStack and so isn't able to be handled by existing ServiceStack's event hooks or user-defined custom logic, so we allow users to modify the behavior in this case via CustomHttpHandlers.

The Error Handling wiki describes how to handle errors in ServiceStack.

Though it might make sense (since it's opt-in) to allow a fallback after the exception is handled to allow it to be further handled by rendering it to a user-specified page, that you're expecting to do here.

We'll look at trying to explore something like this in the future. Feel free to add future feature requests like these to ServiceStack's issue list so we don't forget.

Upvotes: 1

Related Questions