asp.net-coreabp-framework

How can I use Format Arguments in BusinessException?


I have noticed the abp localization provide a Format Arguments mechanism to help generate realtime local string by this way, and I want to know how can I do the same thing in calling a BusinessException while all its overloads are not suitable for this purpose.


Solution

  • Please see the documentation: https://docs.abp.io/en/abp/latest/Exception-Handling#exception-localization

    It is possible to set an exception code and data related to the exception. Then ABP automatically localizes the exception message by also using the data arguments you've provided.

    Example exception:

    throw new BusinessException("App:010046")
        .WithData("UserName", "john");
    

    And the related localization entry in the json file:

    "App:010046": "Username should be unique. '{UserName}' is already taken!"
    

    It is not using {0}, {1}... but using parameter names instead.