Skip to content

Commit 6f83518

Browse files
committed
improved samples with new auto-convert apis
1 parent b503ecf commit 6f83518

File tree

6 files changed

+47
-12
lines changed

6 files changed

+47
-12
lines changed

App_Data/app.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
2-
<SexyContent FileVersion="07.00.00" MinimumRequiredVersion="07.04.02" ModuleVersion="16.00.00" ExportDate="2023-05-05T14:29:25.6015329+02:00">
2+
<SexyContent FileVersion="07.00.00" MinimumRequiredVersion="07.04.02" ModuleVersion="16.00.00" ExportDate="2023-05-05T18:43:46.4070429+02:00">
33
<Header>
44
<App Guid="efe3b9cf-1382-40b3-bee2-34becb5351bc" />
55
<Language Default="en-us" />

api/BooksController.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using DotNetNuke.Web.Api; // [DnnModuleAuthorize] & [ValidateAntiForgeryToken]
99
#endif
1010
using System.Linq; // this enables .Select(x => ...)
11+
using ToSic.Sxc.WebApi; // For the [JsonFormatter] (see below)
1112
using ToSic.Eav.DataFormats.EavLight; // For Auto-Conversion (see below)
1213

1314
[AllowAnonymous] // all commands can be accessed without a login
@@ -30,15 +31,22 @@ public dynamic Persons()
3031
});
3132
}
3233

33-
[HttpGet] // [HttpGet] says we're listening to GET requests
34+
[HttpGet]
35+
[JsonFormatter] // this will auto-convert Entities to JSON
3436
public dynamic PersonsAuto()
3537
{
36-
// 2sxclint:disable:v14-no-getservice
37-
var json = GetService<IConvertToEavLight>();
38-
return json.Convert(App.Data["Persons"]);
38+
return App.Data["Persons"];
3939
}
4040

41-
[HttpGet] // [HttpGet] says we're listening to GET requests
41+
[HttpGet]
42+
[JsonFormatter(Casing = Casing.Preserve)] // auto-convert but preserve casing
43+
public dynamic PersonsAutoPreserveCasing()
44+
{
45+
return App.Data["Persons"];
46+
}
47+
48+
49+
[HttpGet]
4250
public dynamic Books()
4351
{
4452
return AsList(App.Data["Books"])

formulas/200 rest and webapi.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Formulas can retrieve data from WebAPIs and REST.
1616
We have various examples of this on other pages, so this page will just repeat the examples from those page.
1717
</p>
18-
@* @Html.Partial("./Part NowUsingV2.cshtml") *@
18+
@Html.Partial("./Part NowUsingV2.cshtml")
1919
</div>
2020
</div>
2121

formulas/Part NowUsingV2.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@inherits Custom.Hybrid.Razor14
22

33
<div class="alert alert-success">
4-
<strong>Important: Now using v2 Formulas</strong>
4+
<strong>Important: Now using Formulas V2 🆕 in 2sxc 16</strong>
55
<br>
6-
The following samples now use the v2 formulas 🆕 in 2sxc 16.
6+
Formulas V2 has intellisense, stoppability, support for promises and more.
77
</div>

webapi/310-app-data.cshtml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
<p>In this example, we'll get app-data from the WebApi.</p>
1717

1818
<p>Click to see the result of a WebApi call with the shared code:</p>
19-
</div>
19+
20+
@Html.Partial("./Part NowUsingJsonFormatter.cshtml")
2021
</div>
22+
</div>
2123

2224

2325

@@ -27,9 +29,14 @@
2729
</button>
2830
<button type="button" class="btn btn-primary" onclick="getBooks(this)">
2931
Get Books Custom
30-
</button>
32+
</button>
33+
<br>
34+
<br>
3135
<button type="button" class="btn btn-primary" onclick="getPersonsAuto(this)">
32-
Get Persons with Auto-Convert
36+
Persons with Auto-Convert
37+
</button>
38+
<button type="button" class="btn btn-primary" onclick="getPersonsAutoPreserveCasing(this)">
39+
Persons with Auto-Convert, preserve casing
3340
</button>
3441
@* 2sxclint:disable:no-inline-script *@
3542
<script>
@@ -53,13 +60,24 @@
5360
}
5461
function getPersonsAuto(moduleContext) {
5562
$2sxc(moduleContext).webApi.fetchJson('books/personsAuto')
63+
.then(function(results) {
64+
alert('On WebApi with Auto-Convert: Found ' + results.length + ' persons. \n'
65+
+ 'The first one is "' + results[0].firstName + ' ' + results[0].lastName + '"\n\n'
66+
+ 'The raw JSON: \n' + JSON.stringify(results)
67+
);
68+
});
69+
}
70+
71+
function getPersonsAutoPreserveCasing(moduleContext) {
72+
$2sxc(moduleContext).webApi.fetchJson('books/PersonsAutoPreserveCasing')
5673
.then(function(results) {
5774
alert('On WebApi with Auto-Convert: Found ' + results.length + ' persons. \n'
5875
+ 'The first one is "' + results[0].FirstName + ' ' + results[0].LastName + '"\n\n'
5976
+ 'The raw JSON: \n' + JSON.stringify(results)
6077
);
6178
});
6279
}
80+
6381
</script>
6482
@Sys.SourceCode.ResultEnd("file:../api/BooksController.cs")
6583

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@inherits Custom.Hybrid.Razor14
2+
3+
<div class="alert alert-success">
4+
<strong>Important: WebApi using <code>[JsonFormatter]</code> 🆕 in 2sxc 16</strong>
5+
<br>
6+
The <code>[JsonFormatter]</code> has a few effects.
7+
It auto-defaults to camelCase (but you can modify this if you need to preserve casing).
8+
It will also auto-convert entities to a standard JSON format.
9+
</div>

0 commit comments

Comments
 (0)