I'm trying to find a way to use Toastr to display errors to users as Exceptions or Errors occur within my application. The problems I'm running into seem to suggest that it would not be possible to have an exception that occurs in the Controller, or Data Access layer displayed in the current view using Toastr.
I'm wondering if any of you have run into this scenario and what your solution to it was?
What I'm trying to accomplish is that any time there is an unhandled exception, or someone handles an exception manually that we have the ability to display the error to the user without disrupting workflow. Toastr was suggested to me, but being completely javascript I'm not sure the best way to implement it within my MVC4 application.
One option I'm exploring is setting up my default index controller to handle an incoming error string so that I can redirect to it from the Application_Error method in the Global.asax.cs in order to give a friendly redirect, and then if that incoming string is not null then I can use toastr on the Index view. However this is not ideal because it requires a redirect, and disrupts workflow. Also it will not allow for me to display an error without having to thrown an exception or do all my error handling within the javascript.
Other important information is that we are using Telerik Kendo UI, and Razor Syntax if that would help me in any way.
For those of you who have this same question that I had here is the solution:
I found the first step of my solution here: https://github.com/martijnboland/MvcNotification
He had implemented his own form of Notification. But I wanted to be able to use Toastr, or any other kind of Notification options that were out there.
NOTE: Anywhere you see a class that ends in "Res" it's a resource file. This is to keep our strings in our application more organized. That way nobody gets mixed up with that.
Here is how I implemented my solution. NOTE: This works with MVC5 as well
First thing to do is the create a Toastr object in your source code. This will be used to pop the message to the user in the UI eventually.
This uses two special enumerations I created for the Toastr display locations, and message window types so that they could be dynamic.
And
Once you've created the Toastr class you have to override the OnException method of your Controller. There is another way this has to happen if you are using an ApiController which I will also show.
Also you will need to create a ToastrProperties class, seen below.
Controller Example:
I suggest creating a special base class for your controllers so that they all inherit from it, and it can help with other things later in your application. Here is my base controller class.
After you've added this to your project just set your controllers to derive from this class instead of Controller, and that will set this method up.
WebAPI Controller Example:
This one is a little more involved because you can't just inherit from the ApiController class like in the above example. You have to create an Exception Filter Attribute that you would apply to each ApiController. I will show you how you can do it without manually applying it since you will want it on every controller anyways most likely.
First you have to create the Filter Attribute:
Next you need to add your Filter Attribute to all of your Api Controllers. The easiest way to do this is to go into your "WebApiConfig.cs" file, and inside of the Register method just put:
This will setup your WebApi Controllers.
NEXT Step
After you've added either/both methods you need to do a few other things.
First before we go into that though it's important to let you know that what we are doing here in these two methods are actually handling the errors, and logging them within our system. Then we are using the Toast objects static methods to serialize and deserialize JSON into the response/ temp headers of the request so that it's then passed back to the client as JSON and can be handled by the browser upon both async, or post back page requests. But we will get to that in a second.
Because I didn't want this to only be used for passing exception messages to the client I also setup extensions for both the BaseController, and ApiController methods so that they could call a "ShowMessage" method and send Toastr methods down to the client.
Here is the Base Controller version of the Extension:
Here is the Web Api version of the same extension:
Like any standard extension you need to make sure to have the namespace included otherwise it won't work.
Final Step:
Install the Toastr NUGET Package, or get it online, and make sure that it's added to your bundles, or the method you are using to add scripts to your Views.
Now you need to add the Javascript to the _Layout.cshtml in your application.
This requires some explanation. The first function in the script loads the initial response / temp headers because on the initial page load there isn't a standard request that is triggered within the page. Or at least I couldn't find one that would allow access to the headers. So these are placed in using Razor.
The rest should be pretty straight forward. It uses the JSON to pop a toastr message, and adds events to the Ajax requests so that any Toastr messages that come back to it are handled properly.
I'm pretty sure I've got everything in here. If you have any questions, or something is missing when you try to implement it, post on here or PM me and I'll update my post. I hope this helps others who are attempting to do the same thing. :)
Enjoy!