This tutorial shows how to implement a simple action that will draw a QR Code on the current document with a specified value in a C# REST service project with ASP.NET Web API 2 from the .NET Framework.
Client/Server coming and going with Custom Actions
Add and implement the JavaScript function that will call the PostCustomServerAction function from the DocuVieware™ JavaScript public API that is documented here.
It is usually called by a "onclick" event on some other page element like a button.
First, you need to implement the method that will actually draw the QR code on the open document. Add a CustomActions.cs class to your project with the following content:
Now you need to properly catch the DocuVieware™ custom action event in the Global.asax.cs file and implement the corresponding handler.
Step 2 : In the Application_Start part, subscribe to the CustomAction event and redirect it to a private handler named CustomActionsDispatcher (for the sake of clarity but it could be named differently).
From now on, each time the PostCustomServerAction JavaScript function is called client side, the CustomAction event is fired and caught to go into the CustomActionsDispatcher method.
Step 3 : Implement CustomActionsDispatcher that will dispatch the action to the proper method according to the e.actionName value.
Let's have a closer look at the PostCustomServerAction JavaScript function:
There are two extra parameters that you did not used in the previous example that are success and error.
These two callbacks are used to run your own code on success or on error, they are optional functions you need to write on your own depending on what you want to achieve.
The important part here is the success callback, this is through this one that you will be able to retrieve a result from the server action.
Step 1 : In your JavaScript drawQRCode function, slightly change the parameters you are sending: you need it to contain the code value but also the result, simply call it params.
Then you can use it in the PostCustomServerAction call together with the success callback that you need to define, like this:
From now on, each time the custom action is called and successful, we want the result JSON object to be written to the console.
Step 2 : Define the structure of the new parameter in CustomActions.cs, it has to exactly match the JavaScript object structure you previously defined in the drawQRCode function.
Step 3 : Set up the server side handler. To keep things simple, just change the boolean value called example from false to true. In the end, your CustomActions.cs looks like this:
Each time the client side button is clicked, the result parameter is sent server side with false value, it is changed to true and sent back to client side to be displayed in the console.