Search code examples
c#asp.net-web-apidotnetnuke2sxcasp.net-apicontroller

Is this a bug? Or, how do I debug a 2sxc ApiController?


I've got an 2sxc App named chv01, made one Content-Type named Location with a few fields, created a folder named Portals/0/2sxc/chv01/api, added LocationsController.cs which looks like this:

    using DotNetNuke.Web.Api;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Web.Http;
    public class locationsController : ToSic.Sxc.Dnn.ApiController
    {
      [HttpGet]
      [AllowAnonymous]
      public object Location()
      {
        return new ToSic.Sxc.Conversion.DataToDictionary(Edit.Enabled)
          .Convert(App.Data["Location"]);
      }
    }

I've set permissions on the Content-Type so .Anonymous can Read. Everything above came from reading this https://docs.2sxc.org/how-to/webapi/dotnet-webapi.html
Using DNN 9.07.02, 2sxc 11.06.01 My URL is api/2sxc/app/chv01/api/locations/Location Which gives me this error:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "Object reference not set to an instance of an object.",
  "ExceptionType": "System.NullReferenceException",
  "StackTrace": "   
at ToSic.Sxc.Dnn.Code.DnnDynamicCode.Init(IBlock block, ILog parentLog, Int32 compatibility) 
in C:\\Projects\\2sxc\\2sxc\\Src\\Dnn\\ToSic.Sxc.Dnn\\Dnn\\Code\\DnnDynamicCode.cs:line 22\r\n   
at ToSic.SexyContent.WebApi.DynamicApiController.Initialize(HttpControllerContext controllerContext) 
in C:\\Projects\\2sxc\\2sxc\\Src\\Dnn\\ToSic.Sxc.Dnn.WebApi\\WebApi\\DynamicApiController.cs:line 43\r\n   
at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n   
at System.Web.Http.Tracing.Tracers.HttpControllerTracer.<ExecuteAsyncCore>d__5.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n 
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n 
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}

If I change the url to api/2sxc/app/chv01/api/NOTlocations/Location As expected I get this:

<Error>
<Message>2sxc Api Controller Finder: Controller NOTlocationsController not found in app.</Message>
</Error>

So my questions are:

  1. How do I debug this?
  2. Does anyone have a working example?
  3. Any suggestions for a step or detail I missed?
  4. Maybe I found a bug?

Solution

  • So to start, your problem is almost certainly this: since you're using [HttpGet] you probably think you can call the url in the browser and see what happens.

    Now internally DNN does some magic so the API knows what page/module you are on, and this needs some headers in the HTTP. If you're calling the endpoint with javascript you either have to add these headers yourself, or use $2sxc(...) helper to get this to work. You can find examples in this app: https://2sxc.org/en/apps/app/tutorial-javascript-rest-api-using-jquery-and-angularjs

    That's just to help you fix your issue.

    To debug you have two options

    1. use 2sxc insights - and work with Log objects
    2. attach visual studio to DNN and debug live

    make sense?