I want to implement toastr.js in dotvvm. I created an exception filter which catch all the exception errors on the viewmodel:
public class ErrorLoggingActionFilter : ExceptionFilterAttribute
{
protected override async Task OnPageExceptionAsync(IDotvvmRequestContext context, Exception exception)
{
//context.CustomResponseProperties.Add("Error", exception.Message);
// Inject JavaScript code to display toastr notification
// Get the exception message
string errorMessage = exception.Message;
// Inject JavaScript code to display toastr notification
string toastrScript = $"<script>toastr.error('{errorMessage}');</script>";
context.ResourceManager.AddInlineScript(toastrScript);
// Mark the exception as handled
context.IsPageExceptionHandled = true;
await base.OnPageExceptionAsync(context, exception);
}
}
but i'm having hard time adding the toastr.js and implementing it.
You should use
OnCommandExceptionAsyncinstead ofOnPageExceptionAsync.Additionally, the
AddInlineScriptrequires just the JS code without the<script></script>tags:Also, be careful when building scripts using the string concatenation. If the error message contains apostrophe, it would end the string literal and interpret the rest as a script code. If an attacker finds a way how to inject their input into the error message, you would make a script injection vulnerability in your app.
I've added
KnockoutHelper.MakeStringLiteralto escape characters in the error message.Finally, don't forget to register all script and style dependencies in
DotvvmStartup: