Requirements
Bare Metal WebAPI Commands (v12.10+)
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.
Headers
This will show you the headers which will probably be needed for a request:
Output
Note about Hybrid WebAPIs
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
URL
Resolve URLs to endpoints and similar
Output
Source Code of this file
Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code
@inherits Custom.Hybrid.Razor14 <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2>Bare Metal WebAPI Commands (v12.10+)</h2> <p> 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 <strong>Bare Metal WebApi</strong> since it gives you the stuff so you can do the work yourself. </p> <p> As with everything, you usually need to get the module <code>sxc</code> 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 <code>sxc</code> controller, but there are other ways as well if you prefer working the the Module Id. </p> </div> </div> <h3>Headers</h3> <p> This will show you the headers which will probably be needed for a request: </p> </trim> <button type="button" class="btn btn-primary" onclick="showHeaders(this)">Default headers</button> <button type="button" class="btn btn-primary" onclick="showHeaders(this, 'GET')">Headers for GET</button> <button type="button" class="btn btn-primary" onclick="showHeaders(this, 'POST')">...for POST</button> <button type="button" class="btn btn-primary" onclick="showHeaders(this, 'PUT')">...for PUT</button> @* 2sxclint:disable:no-inline-script *@ <script> function showHeaders(moduleContext, optionalVerb) { var headers = $2sxc(moduleContext).webApi.headers(optionalVerb); alert('These are the headers used if the verb is "' + (optionalVerb ?? '') + '":\n' + JSON.stringify(headers, null, 2)); } </script> @Html.Partial("MessageHybrid.cshtml") <h3>URL</h3> <p> Resolve URLs to endpoints and similar </p> <button type="button" class="btn btn-primary" onclick="showUrl(this, 'basic/hello')">Url for: basic/hello</button> <button type="button" class="btn btn-primary" onclick="showUrl(this, 'app/auto/api/basic/hello')">Url: app/auto/api/basic/hello</button> <button type="button" class="btn btn-primary" onclick="showUrl(this, 'basic/square?number=2742')">Url: basic/square?number=2742</button> @* 2sxclint:disable:no-inline-script *@ <script> function showUrl(moduleContext, url) { var finalUrl = $2sxc(moduleContext).webApi.url(url); alert('These is the full url for "' + url + '":\n' + finalUrl); } </script> @* TODO: @2dm examples with url-parameters object *@ @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })