Implementing a more complex WebAPI

Dec 10, 2015 at 10:15 PM
Edited Dec 10, 2015 at 10:15 PM
Being able to drop a WebAPI controler .cs file into the API folder and have it automatically included in DNN/2sxc is nice, but what if we want to have a more complicated WebAPI with references to DLLs and/or Service references or similar? Let's say we want to build a public facing WebAPI that then internally calls WCF services from back end via a Service Reference, how would you set up that in 2sxc (or DNN Service Plattform for that matter)? Or if you just want to include a DLL for whatever reason to consume in the WebAPI?
Dec 12, 2015 at 9:01 PM
So, can this be done with 2sxc? Looked at the WebAPI examples but they are quite "simple" in that they don't call any external code like in Entity Framework or similar? Or will this "automatic WebAPI" only work when no references are needed to other DLLs and similar?
Dec 12, 2015 at 10:30 PM
Edited Dec 12, 2015 at 10:37 PM
A related question, will neat 2sexy functions like :

$2sxc(modId).webApi.get('Categories/All')

still work even if we use DNNs Service Framework and build our WebAPI by placing DLL:s in the DNN /bin folder instead of the 2sxc WebAPI by putting the controler in API folder?

Also noted that latest 2sxc AngularJS apps seem to use:

svc.server = content('Feedback');
svc.server.get(id)

instead of the:

$2sxc(modId).webApi.get('Categories/All')

Should we skip $2sxc?
Coordinator
Dec 14, 2015 at 11:24 AM
You should use $2sxc, as that API should stay stable, but the underlying api (even the urls to access the target) and the security mechanisms could change.


In angular, 2sxc exits as 2 optional dependencies: sxc and $2sxc. sxc is already mapped to the current module/id.

internally we have many other apis but these are not published, so we may change them any time we need to improve the system. So you shouldn't use those, as they may change.
Coordinator
Dec 14, 2015 at 11:26 AM
Regarding your first question you can basically use anything .net provides - from within 2sxc.

If you want to create other apis outside of 2sxc that's fine as well, it just can't be deployed as export/import app and other features will not work. This is ok for single installations.
Dec 14, 2015 at 12:28 PM
2sicDev wrote:
You should use $2sxc, as that API should stay stable, but the underlying api (even the urls to access the target) and the security mechanisms could change.


In angular, 2sxc exits as 2 optional dependencies: sxc and $2sxc. sxc is already mapped to the current module/id.

internally we have many other apis but these are not published, so we may change them any time we need to improve the system. So you shouldn't use those, as they may change.
Thanks for the info.

(And as always, is there any way to not have to enter a CAPTCHA every time I post here?)
Dec 14, 2015 at 12:30 PM
2sicDev wrote:
Regarding your first question you can basically use anything .net provides - from within 2sxc.

If you want to create other apis outside of 2sxc that's fine as well, it just can't be deployed as export/import app and other features will not work. This is ok for single installations.
OK, great. How would we then go about to achieve this, by compiling a DLL into DNNs /bin as normal with DNNs Service Framework and then call it using 2sxc but not put anything in /API under 2xc?
Dec 14, 2015 at 2:11 PM
Trying to use the $2sxc but have a strange problem. I've installed and tried the "Tutorial WebAPI with AngularJS" which works fine. Then I decided to make a "copy" of it so I created a new application, created a view and then just copied the contents of "1 Api Calls with AngularJS Controller.html" to the new file and then the "assets" and "API" folders of the to my new app. They should now be exactly the same despite having different appnames and I use the same DNN module to switch between the 2 apps (same moduleid and tabid).

But now the strange part occurs, this code:

$2sxc($scope.moduleId).webApi.get('Categories/All')
            .then(function (result) {
                $scope.Categories = result; // attach categories to view model
                $scope.SelectedCategory = $scope.Categories[0];
                $scope.$apply();            // tell AngularJS to update view
            });
in both apps tries to access the URL:

http://gibsportaldevelopment.improwise.local/DesktopModules/2sxc/API/app-api/Categories/All

but with the app "Tutorial WebAPI with AngularJS" it works but with the new app it gives an Internal Server error despite URL and ModuleID being identical. Have tried to debug 2sxc some but I can't find the reason why the same call works with one app but not the other.

Any ideas?
Coordinator
Dec 15, 2015 at 11:03 AM
Edited Dec 23, 2015 at 11:51 AM
I'm guessing the angular example has a content-type "Categories" and you probably don't
Dec 22, 2015 at 7:24 AM
improwise, as you wrote:
still work even if we use DNNs Service Framework and build our WebAPI by placing DLL:s in the DNN /bin folder instead of the 2sxc WebAPI by putting the controler in API folder?

We already did exactly that, we've made our own library working with three layers of cs-classes:
helpers: doing different tasks that we need multiple times (making a zip file, db-connections etc...)
api: actual controllers (not webapi, but doing all the work, controllers do)
2sxc: webapi controllers, but "layers" who are extending SxcApiController just returning the same function from the api controller, we include the .js-services here too.

With that structure, we are able to unit-test everything with a demoapp.
Whatever controller we need for an app, we just copy the 2sxc layer controller into the api folder of the app.

About the svc.server = content('Feedback');
as 2sicDev wrote, these are 2sxc content type calls, not webapi calls.