ServiceStack renders RequestBindingException via Razor template

只愿长相守 提交于 2019-12-12 03:28:25

问题


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?


回答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.



来源:https://stackoverflow.com/questions/33080237/servicestack-renders-requestbindingexception-via-razor-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!