Requirements
WebAPI Basics: Call WebAPIs and REST using JavaScript
Very Basic WebAPI Samples
In this example, we'll assume your WebApi controller is called Basic
(so the file is called BasicController.cs
and has a class called BasicController
). It's located in /api
which is the default location for WebApi controllers.
We'll show various usages both with the new webApi.fetchJson(...)
and webApi.fetch(...)
standard fetch
- with the new
webApi.fetchJson(...)
andwebApi.fetch(...)
commands (new in v12.10) - with the browser built in
fetch
- using jQuery (not recommended any more, because we believe jQuery is dying)
- another example calling the endpoint using a parameter to calculate something so you can see how to use url parameters.
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
More secure Basic WebApi Example
In this example, we'll ensure that calls to your API only come from users who are visiting your website. This is to protect agains CSRF attacks, where another website gets your users to interact with your API. That could be dangerous, because maybe your API has things only a logged in user should see - and without the CSRF protection, the other website could get access to that because the user is logged in.
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
REST APIs for Content using GET, POST, PUT, DELETE Verbs
In this example, we'll assume your WebApi controller is called Orders
(so the file is called OrdersController.cs
and has a class called OrdersController
). It's located in /api
which is the default location for WebApi controllers.
We'll show various usages of the http methods with the new webApi.fetch(...)
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
Bare Metal WebAPI Commands
In some cases you want to have more control over the API calls, but still need 2sxc to help you get the right headers and urls. This is what we call the Bare Metal WebApi since it gives you the stuff so you can do the work yourself.
As with everything, you usually need to get the module sxc
controller first, so that the values you get match your app and use case. In these examples, we're using the current HTML-node to find the sxc
controller, but there are other ways as well if you prefer working the the Module Id.
This will show you the headers which will probably be needed for a request.
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if
Resolve URLs to endpoints and similar.
⬇️ Result | Source ➡️
Note about Hybrid APIs
These examples are all done to work in both Dnn ☢️ and Oqtane 💧. Because of this, the Api Controller have special conditional statements like #if NETCOREAPP
which uses different namespaces in .net core and .net Framework.
If you only target Dnn or Oqtane, you can remove the lines you don't need. See See how to use #if