Open Closed

How to return error message in a Razor page's OnPostAsync Method #4104


User avatar
0
coop1 created

Hi there,

I have a Razor form that is creating a new entry in the database with a Name field that must be unique. I am performing server-side validation in the OnPostAsync method and if the submitted Name entry already exists in the database, the method returns an error. The returned error object is a Microsoft.AspNetCore.Mvc.ContentResult with the StatusCode set to 400 and the Content set to an error message stating the submitted name already exists, this causes the ABP "An error has occurred!" modal to pop up but the message is not displayed, and instead the modal just says "Error detail not sent by server.". How do I properly return an error from the server side so that my custom error message can be displayed?

Thank you!


1 Answer(s)
  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    If an exception implements the IUserFriendlyException interface, then ABP does not change it's Message and Details properties and directly send it to the client.

    You can use UserFriendlyException for this. For more information, I recommend you look here.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09