specimen
specimen

Reputation: 1765

ServiceStack renders RequestBindingException via Razor template

I have a simple DTO with just an Int inside:

[Route("/cells/{Id}")]
public class CellDetail 
{
    public int Id { get; set; }
}

Using a URL like /cells/abc gives med a RequestBindingException in JSON, but the HTML view is still rendered via my .cshtml file, which obviously fails due to @Model == null.

Shouldn't it give a HTML error page instead? How does it even know which .cshtml view to select?

Upvotes: 0

Views: 121

Answers (1)

mythz
mythz

Reputation: 143319

The Razor Page lets you access the Error Info about the Request in:

object ModelError { get; }       //The Error Response object
bool IsError { get; }            //ModelError != null
ResponseStatus GetErrorStatus(); //ModelError in ResponseStatus
MvcHtmlString GetErrorMessage(); //Just the Error Message to display

Which lets you render the same page but show the above error info in a UX friendly way.

A handy short-cut you can add to your page to just display the Error message is to

@if (RenderErrorIfAny()) { return; }

Which short-circuits the page and displays the error message in a bootstrap-friendly format which looks like:

<div id="error-response" class="alert alert-danger">
    <h4>{ErrorCode} : {Message}</h4>
    <pre>{StackTrace}</pre>
</div>

You can also add CSS to modify how the error looks.

Upvotes: 1

Related Questions