Skip to main content
Home  › ... Razor

WebApi Tutorials

Tutorial HomeWebApi

Very basic WebApi Examples

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(...)

Click to see the result of a WebApi call with the shared code:

Output

<button type="button" class="btn btn-primary" onclick="getOrdersFetch(this)">Quick Fetch GET "Orders"</button> 
  @* 2sxclint:disable:no-inline-script *@
<script>
    function getOrdersFetch(moduleContext) {
      $2sxc(moduleContext).webApi.fetchJson('orders/get')
        .then(data => alert('Result using quick Fetch: ' + JSON.stringify(data)));
    }
</script>

Source Code of ../api/OrdersController.cs

// Add namespaces for security check in Oqtane & DNN despite differences in .net core/.net Framework
// If you only target one platform, you can remove the parts you don't need
#if NETCOREAPP
using Microsoft.AspNetCore.Authorization; // .net core [AllowAnonymous] & [Authorize]
using Microsoft.AspNetCore.Mvc;           // .net core [HttpGet] / [HttpPost] etc.
#else
using System.Web.Http;                    // .net 4.5 [AllowAnonymous] / [HttpGet]
using DotNetNuke.Web.Api;                 // [DnnModuleAuthorize] & [ValidateAntiForgeryToken]
#endif
using System.Collections.Generic;

[AllowAnonymous]                          // all commands can be accessed without a login
public class OrdersController : Custom.Hybrid.Api14 // see https://r.2sxc.org/CustomWebApi
{
  public List<Order> orders = new List<Order>{
    new Order{ Id = 1, Amount = 10 },
    new Order{ Id = 2, Amount = 37 }
  };

  [HttpGet]        // [HttpPost] says we're listening to POST requests
  public List<Order> Get()
  {
    return orders;
  }

  [HttpPost]        // [HttpPost] says we're listening to POST requests
  public Order Post([FromBody] Order order)
  {
    order.Id = orders[orders.Count - 1].Id + 1;
    orders.Add(order);
    return orders[orders.Count - 1];
  }

  [HttpPut]        // [HttpPut] says we're listening to PUT requests
  public Order Put([FromBody] Order order)
  {
    orders[0].Amount = order.Amount;
    return orders[0];
  }

  [HttpDelete]        // [HttpDelete] says we're listening to DELETE requests
  public string Delete()
  {
    orders.RemoveAt(0);
    return "";
  }
}

public class Order {
  public int Id;
  public int Amount;
}

// The next line is for 2sxc-internal quality checks, you can ignore this
// 2sxclint:disable:no-dnn-namespaces - 2sxclint:disable:no-web-namespace

Output

<button type="button" class="btn btn-primary" onclick="postOrdersFetch(this)">Quick Fetch POST "Orders"</button> 
  @* 2sxclint:disable:no-inline-script *@
<script>
  function postOrdersFetch(moduleContext) {
    $2sxc(moduleContext).webApi.fetchJson('orders/post', { amount: Math.floor(Math.random() * 100) })
      .then(data => alert('Result using quick Fetch: ' + JSON.stringify(data)));
  }
</script>
To see the source of the WebApi Controller, check the first sample on this page.

Output

<button type="button" class="btn btn-primary" onclick="putOrdersFetch(this)">Quick Fetch PUT "Orders"</button> 
  @* 2sxclint:disable:no-inline-script *@
<script>
  function putOrdersFetch(moduleContext) {
    $2sxc(moduleContext).webApi.fetchJson('orders/put', { amount: Math.floor(Math.random() * 100) }, 'PUT')
      .then(data => alert('Result using quick Fetch: ' + JSON.stringify(data)));
  }
</script>
To see the source of the WebApi Controller, check the first sample on this page.

Output

<button type="button" class="btn btn-primary" onclick="deleteOrdersFetch(this)">Quick Fetch DELETE "Orders"</button> 
  @* 2sxclint:disable:no-inline-script *@
<script>
  function deleteOrdersFetch(moduleContext) {
    $2sxc(moduleContext).webApi.fetchJson('orders/delete', null, 'DELETE')
      .then(data => alert('Delete executed'));
  }
</script>
To see the source of the WebApi Controller, check the first sample on this page.

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>Very basic WebApi Examples</h2>
      <p>
        In this example, we'll assume your WebApi controller is called <code>Orders</code> (so the file is called <code>OrdersController.cs</code> and has a class called <code>OrdersController</code>). It's located in <code>/api</code> which is the default location for WebApi controllers. <br>
        We'll show various usages of the http methods with the new <code>webApi.fetch(...)</code>
      </p>

      <p>
        Click to see the result of a WebApi call with the shared code:  
      </p>
    </div>
  </div>

  <button type="button" class="btn btn-primary" onclick="getOrdersFetch(this)">Quick Fetch GET "Orders"</button> 
    @* 2sxclint:disable:no-inline-script *@
  <script>
      function getOrdersFetch(moduleContext) {
        $2sxc(moduleContext).webApi.fetchJson('orders/get')
          .then(data => alert('Result using quick Fetch: ' + JSON.stringify(data)));
      }
  </script>


@Html.Partial("MessageHybrid.cshtml") 

  <button type="button" class="btn btn-primary" onclick="postOrdersFetch(this)">Quick Fetch POST "Orders"</button> 
    @* 2sxclint:disable:no-inline-script *@
  <script>
    function postOrdersFetch(moduleContext) {
      $2sxc(moduleContext).webApi.fetchJson('orders/post', { amount: Math.floor(Math.random() * 100) })
        .then(data => alert('Result using quick Fetch: ' + JSON.stringify(data)));
    }
  </script>


  <button type="button" class="btn btn-primary" onclick="putOrdersFetch(this)">Quick Fetch PUT "Orders"</button> 
    @* 2sxclint:disable:no-inline-script *@
  <script>
    function putOrdersFetch(moduleContext) {
      $2sxc(moduleContext).webApi.fetchJson('orders/put', { amount: Math.floor(Math.random() * 100) }, 'PUT')
        .then(data => alert('Result using quick Fetch: ' + JSON.stringify(data)));
    }
  </script>


  <button type="button" class="btn btn-primary" onclick="deleteOrdersFetch(this)">Quick Fetch DELETE "Orders"</button> 
    @* 2sxclint:disable:no-inline-script *@
  <script>
    function deleteOrdersFetch(moduleContext) {
      $2sxc(moduleContext).webApi.fetchJson('orders/delete', null, 'DELETE')
        .then(data => alert('Delete executed'));
    }
  </script>



@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })