1+ @inherits ToSic .Sxc .Dnn .RazorComponent
12<hide >
23@RenderPage( "_header.cshtml")
34</hide >
45
56<trim >
67<h2 >Reuse code with .cs files (new in 2sxc 10.01)</h2 >
78<p >
8- Starting with 2sxc 10.01 <code >CreateInstance()</code > can also be used with .cs files,
9- from both razor pages as well as WebApi controllers.
9+ Starting with 2sxc 10.01 <code >CreateInstance()</code > can also be used with .cs files,
10+ from both razor pages as well as WebApi controllers.
1011 This allows you to create shared code which can be used in Razor and WebApi controllers.
1112</p >
1213</trim >
1516<h2 >Using shared library of simple functions</h2 >
1617<trim >
1718<p >
18- The example takes a cs file <code >FunctionsBasic.cs</code > with shared code in a class <code >FunctionsBasic</code >.
19+ The example takes a cs file <code >FunctionsBasic.cs</code > with shared code in a class <code >FunctionsBasic</code >.
1920 This example uses a simple C# class, so the code only has what's in it.
2021</p >
2122</trim >
4041<h2 >Using shared code with context</h2 >
4142<trim >
4243<p >
43- Often you may need context - like the <code >Dnn</code > or <code >App</code > objects.
44+ Often you may need context - like the <code >Dnn</code > or <code >App</code > objects.
4445 We made this easy by defining a base class you can inherit from, called <code >ToSic.Sxc.Dnn.DynamicCode</code >.
4546
46- If you use that as your base-class, all existing context is automatically attached, allowing you to
47+ If you use that as your base-class, all existing context is automatically attached, allowing you to
4748 access variables like <code >App</code >.
4849</p >
4950</trim >
5859<hr >
5960<h2 >Using shared libraries in WebApi Controllers</h2 >
6061<p >
61- The mechanism above also works in WebApi Controllers (if you have 2sxc 10.01+).
62- Note that specifically in WebApi you can only use <code >CreateInstance(...)</code > for <code >.cs</code > files.
62+ The mechanism above also works in WebApi Controllers (if you have 2sxc 10.01+).
63+ Note that specifically in WebApi you can only use <code >CreateInstance(...)</code > for <code >.cs</code > files.
6364</p >
65+ </hide >
66+
67+ @* tell 2sxc that we'll use the js API *@
68+ @Edit.Enable(js :true)
69+
6470<blockquote >
65- Click to see the result of a WebApi call with the shared code:
71+ Click to see the result of a WebApi call with the shared code:
6672 <button type =" button" class =" btn btn-primary" onclick =" callApiWithSharedCode(this)" >
6773 Call WebApi
68- </button >
74+ </button >
6975</blockquote >
70- </ hide >
76+
7177<script >
7278 function callApiWithSharedCode (context ) {
7379 $2sxc (context).webApi .get (' app/auto/reuse/api/sharedcode/hello' )
7480 .then (function (results ) {
7581 console .log (results);
76- alert (results);
82+ alert (results);
7783 });
7884 return false ;
7985 }
8086 </script >
8187
8288<hide >
83- @RenderPage( "../shared/_source-code.cshtml", new { Path = VirtualPath } )
89+ @RenderPage( "../shared/_source-code.cshtml", new { Path = VirtualPath } )
8490
85- @RenderPage( "../shared/_source-code.cshtml", new {
91+ @RenderPage( "../shared/_source-code.cshtml", new {
8692 Path = VirtualPath ,
8793 File = " FunctionsBasic.cs" ,
88- Size = 400 ,
94+ Size = 400 ,
8995 Language = " csharp"
90- } )
96+ } )
9197
92- @RenderPage( "../shared/_source-code.cshtml", new {
98+ @RenderPage( "../shared/_source-code.cshtml", new {
9399 Path = VirtualPath ,
94100 File = " FunctionsWithContext.cs" ,
95- Size = 400 ,
101+ Size = 400 ,
96102 Language = " csharp"
97- } )
103+ } )
98104
99- @RenderPage( "../shared/_source-code.cshtml", new {
105+ @RenderPage( "../shared/_source-code.cshtml", new {
100106 Path = VirtualPath ,
101107 File = " api/SharedCodeController.cs" ,
102- Size = 400 ,
108+ Size = 400 ,
103109 Language = " csharp" ,
104110 TitlePath = " /reuse/" ,
105- } )
111+ } )
106112</hide >
0 commit comments