0

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?

specimen
  • 1,735
  • 14
  • 23

1 Answers1

1

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • So this must be placed on top of every View-page? (_Layout page doesn't work, as it's probably called after the view-page.) – specimen Oct 12 '15 at 14:58