Developer Center
Saber.Core Functionality
If you are developing a plugin for Saber, please take the time to read and understand all of the documentation on this page. Saber.Core is a namespace within the Saber.Core NuGet package that allows developers to access various features within Saber when building a vendor plugin.
App
The static App class contains information about Saber's environment.
-
App.Environment will return the environment that Saber is running in, which can include
development, staging, or production.
To change the current environment for Saber, open the file web.config located in the Saber application folder and alter the following line: <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
-
App.Host will return the current host URI
(e.g. http://localhost:7000/ or https://saber.datasilk.io/).
To change the current host URI for Saber, open the file config.json located in the Saber application folder and alter the property "hostUri". Make sure to include a forward slash / at the end of your URI.
- App.IsDocker will return true if Saber is running in a Docker container and will also return true if running in a Linux environment.
- App.RootPath will return the absolute directory structure to the root of your Saber application folder (e.g. "C:\websites\Saber" in Windows or "/apps/Saber" in Linux)
- App.MapPath(string path) will return the absolute directory structure to the relative path defined in the path argument.
IRequest
The IRequest interface contains information about the current request and is inherited by both the Controller and Service classes. These classes inherit the Request class, which is part of the Datasilk Core MVC framework.
- IRequest.CheckSecurity(string key = "") will return true if the authenticated user is part of a Security Group that has access to the desired security key.
- IRequest.Context will return the Microsoft.AspNetCore.Http.HttpContext for the current request.
- IRequest.Path will return the URI path for the current request.
- IRequest.PathParts will return the URI path for the current request split into an array separated by forward slashes.
- IRequest.Parameters returns a Dictionary<string, string> that combines any key/value pairs from the request query string as well as any form POST parameters that uses default encoding or multipart/form-data.
- IRequest.Parameters.Files returns a Dictionary<string, FormFile> that contains any binary files contained in the page request form POST that uses encoding types multipart/form-data or application/octet-stream.
-
IRequest.AddScript(string url, string id = "", string callback = "")
is a method used to add a JavaScript file to the response. For the Controller class, this method will
append a <script src="/editor/vendors/my-plugin/myfile.js"></script> tag to the bottom of the web page. For the
Service class, this method can only be used if your service method returns
Response(string html).
The Response method will return a JSON object that will need to be processed using the following JavaScript code:
S.ajax.post('/MyService/MyMethod', data, (response) => {response.selector = '.my-class';S.ajax.inject(response);});
-
IRequest.AddCSS(string url, string id = "")
is a method used to add a CSS file to the response. For the Controller class, this method will
append a <link rel="stylesheet" href="myfile.css"/> tag in the
header of the web page. For the Service class, this method can only be used if your service method
returns Response(string html). The Response
method will return a JSON object that will need to be processed using the following JavaScript code:
S.ajax.post('/MyService/MyMethod', data, (response) => {response.selector = '.my-class';S.ajax.inject(response);});
- IRequest.AccessDenied(string message) will return a string containing your message along with a response code of 403.
- IRequest.Error(string message) will return a string containing your message along with a response code of 500.
- IRequest.Error404(string message) will return a string containing your message along with a response code of 404.
Controller
The Controller class inherits the Request class and is used to handle page requests. URLs within Saber will route to controllers based on the controller class name. For example, the URL http://localhost:7000/dashboard/users routes to the Dashboard controller class.
- Controller.Init() is executed when a user requests the controller's URL route.
-
Controller.Render() is executed after
Init() when a user requests the controller's URL route. This method will return
a string back to the user's web browser with content type text/html and response code 200.
To render the default layout (which includes a <head> tag with platform.css,
dark.css, and platform.js) return
base.Render(html).
public override string Render(string body = ""){var view = new View("/Vendors/MyPlugin/view.html");AddScript("/editor/vendors/myplugin/myplugin.js");return base.Render(view.Render());}
NOTE: You can also create controllers that inherit other controllers and chain their Render methods using base.Render(html).
NOTE: You can only use the AddScript and AddCSS methods if you return base.Render(html).
- Controller.Title sets the page title if you return base.Render(html).
- Controller.Description sets the page description if you return base.Render(html).
- Controller.AccessDenied<T>() will return an instance of the provided Controller along with a response code of 403.
- Controller.Error<T>() will return an instance of the provided Controller along with a response code of 500.
- Controller.Error404<T>() will return an instance of the provided Controller along with a response code of 404.
Service
The Service class inherits the Request class and is used for web API calls. URLs within Saber will route to services based on the URL beginning with /api/, the service class name, and the service method name. For example, the URL http://localhost:7000/api/dashboard/users routes to the Users method within the Dashboard service class.
- Service.JsonResponse(dynamic obj) will return a JSON serialized string of the provided dynamic object.
-
Service.Response(string html) will
return a JSON serialized string of a Reponse object, which contains the following properties:
- css is a string containing the CSS styles that you wish to load
- html is a string containing the HTML code you wish to inject using JavaScript
- javascript is a string containing the JavaScript code that you wish to execute
- selector is a string containing the CSS selector used to inject your html string into
- type is an enum of type responseType, which is responseType.replace by default. This is used to determine how to inject your html string into the web page, which can be replace, append, before, after, or prepend
In JavaScript, you must call your Service and inject the response using the following code:
S.ajax.post('/MyService/MyMethod', data, (response) => {response.selector = '.my-class';S.ajax.inject(response);}); - Service.Success() will return the string success along with a response code of 200.
- Service.BadRequest(string message) will return a string containing your message along with a response code of 400.
User
The User class contains information about the authenticated user and is accessible from both the Controller and Service class.
- User.UserId returns the authenticated user's ID. If the user is not authenticated, the value will be 0.
- User.Email returns the email address of the authenticated user.
- User.Name returns the name of the authenticated user.
- User.Photo returns true if the user has uploaded a profile picture.
- User.DateCreated returns a DateTime of when the authenticated user signed up for Saber.
- User.Language returns the language Id that the authenticated user prefers to use. The default value is "en" which is an abbreviation for English.
- User.Keys is a List of key/value pairs for security keys that the authenticated user has access to. The list can include keys from Core.Security.Keys, which are a list of internal keys used by Saber, and they can include keys from various installed vendor plugins as well. This field is used with Request.CheckSecurity(string key) to check if the authenticated user has permission to access certain features within Saber.
- User.Groups is an int[] array of Security Group Ids that the authenticated user belongs to.
View
The View class is used to render HTML views and is mainly used within Controller and Service classes. HTML views can contain mustache variables, blocks, and includes that are replaced with dynamic content when the view is rendered.
- mustache variables, {{my-var}}, are replaced with data (e.g. view["my-var"] = "cool!";).
- mustache blocks, {{my-block}}<div>cool!</div>{{/my-block}}, will render the HTML contents of the block (e.g. view.Show("my-block");)
- mustache includes, {{header "/Vendor/MyPlugin/header.html"}}, will load a partial view.
NOTE: All mustache variables must be lowercase no matter if they bind to C# fields that have uppercase letters.
-
View[string]
will bind data to a mustache variable within the HTML view. For example, the following mustache variable, {{user-name}}
can be rendered with the following code:
var view = new View("/Vendors/MyPlugin/myplugin.html");view["user-name"] = User.Name;return view.Render();
-
View.Bind(dynamic obj)
will bind C# fields to mustache variables within the HTML view. For example, the following HTML view,
{{user.name}}<img src="/images/users/photo_{{user.userid}}.jpg"/>
can be rendered by binding a C# object with the following code
var view = new View("/Vendors/MyPlugin/myplugin.html");view.Bind(new { user = request.User });return view.Render();
-
View.Child(string id)
will give you access to the mustache variables within a partial view.
var view = new View("/Vendors/MyPlugin/myplugin.html");view.Child("header").Show("has-awards");view.Child("header").Bind(new { awards });view.Child("header")["name"] = User.Name;return view.Render();
-
View.Clear() will clear the view of all bound data. This is useful if you are reusing a View
within a loop.
var view = new View("/Vendors/MyPlugin/item.html");var html = new StringBuilder();foreach (var product in products){view.Bind(new { product });html.Append(view.Render());view.Clear();}return html.ToString();
- View.Render() will render the HTML view to a string and all mustache variables, blocks, and includes will be rendered as well.
The static Email class is used to compose and send emails to users within Saber.
- Email.Create(MailAddress to, string subject, string body, string[] attachments = null) should be used to compose a message to a user within Saber.
-
Email.Send(MailMessage message, string type)
should be used to send a message with the preferred email client (IVendorEmailClient).
Saber will select which email client to use based on the value of type that you provide
along with the Email Action settings configured for the type
(IVendorEmails) located within Saber's Website Settings tab.
For example, the following code configures email types for a Saber plugin:
public class Emails : IVendorEmails{public EmailTypes[] Types { get; set; } = new List<EmailType>(){new EmailType(){Key = "order",Name = "Order",Description = "An email sent when a customer purchases one or more products or services.",TemplateFile = "order.html",UserDefinedSubject = true}}}
and the following code will send an email for that specific EmailType
var message = new Email.Message(new MailAddress(User.Email, User.Name), subject, html);Email.Send(message, "order");
Log
The static Log class is used to log errors and other messages in Saber's database, which can be accessed and used as a way for debugging your plugin and is especially useful in a production environment.
- Saber.Core.Log.Error(Exception ex, IRequest request = null, string area = "") will generate a new record in the database Log_Error table.