Actions
If you want to execute serverside logic on a Create, Read, Update or Delete, there's ServiceLogic. For everything else, there's Actions. You can think of actions as a way to create unique endpoints.
Create an Action
In the services project, in the ServiceActions folder create a new file with the name of your action.
Project.Services / Actions / MyAction.cs
Call an Action
We can then use the following code in our NextJS project to call our action.
src / components / MyComponent.tsx
Parameters
You can pass parameters into the action using the second argument of the authRequest.action function.
src / components / MyComponent.tsx
In our action, we can retrieve these parameters with context.Parameters.
Project.Services / Actions / MyAction.cs
Response Body
To return a json object from the action, we can provide an object to ServiceResult.Success.
Project.Services / Actions / MyAction.cs
If the call to the action succeeded, the data property of the response will contain the response body.
src / components / MyComponent.tsx
Upload File
A file can be sent using FormData.
src / components / MyComponent.tsx
The file can be accessed in the Action using the File property on the ActionServiceContext.
Project.Services / Actions / MyAction.cs
Download File
To send a file for download from an action to the browser, respond with the FileData class.
Project.Services / Actions / MyAction.cs
To call the action and download the file, specify the file name in the 3rd argument of the the call to the action.