Search code examples
c#2sxc

2sxc method App.Data.Create returns System.NullReferenceException


Versions: DNN PLATFORM v. 09.01.00 (367) / 2sic 2SexyContent 09.06.00

Hello,

The external call to the WebAPI App.Data.Create method does not seem to work anymore (it was working with v9.4).

My controller is :

public class gouvGouvernementController : SxcApiController
{
    [HttpPost]
    [AllowAnonymous]
    public bool Create(organism postOrganisme)
    {
        var organisme = new Dictionary<string, object>();
        organisme.Add("Nom", postOrganisme.Nom.ToString());
        organisme.Add("Acronyme", postOrganisme.Acronyme.ToString());

        App.Data.Create("Organismes", organisme, "External system");
        return true;
    }
}


public class organism
{
    public string Nom { get; set; }
    public string Acronyme { get; set; }
}

My ajax call (in a simple html page)

<script>
    var vJson = {
                "Nom": "Organism",
                "Acronyme": "ACRO1"
                };

    $.ajax({
    url:"https://www.mywebsite.net/api/2sxc/app/myapp/api/mycontroller/Create",
    data: vJson,
    type:"POST",
    dataType: 'json',
    success: function(donnees) {
            console.log(donnees);
        }
    }
);
</script>

The response of the method is

{"Message":"Bad Request","ExceptionType":"System.NullReferenceException","ExceptionMessage":"Object reference not set to an instance of an object."}

When I delete the line App.Data.Create("Organismes", organisme, "External system");, I receive a response 200 so the problem seems to be located in this method.

Can you tell me if I do something wrong ? As I already told it, the code was runing fine with 2sic_2SexyContent_09.04.03.

Thank you for your help!

f

EDIT: I just reinstall the version 9.4 and I have the same error so this is not related to the 2sxc version.

Obviously, I have a problem with my code so if someone can give me some advice, I will be really happy!

EDIT2: I installed the version 9.06.01 (labeled 9.06.00) and I still have the same issue. Here is the error stack:

at mycontroller.Create(Message postMessage) at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Tracing.ITraceWriterExtensions.d__181.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Tracing.ITraceWriterExtensions.d__18`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ExceptionFilterResult.d__0.MoveNext()

EDIT3: I tried to add this code in the page:

<a href="javascript:submitFeedback(@Dnn.Module.ModuleID)">test</a>
 <script>
    function submitFeedback(mid) {
        $2sxc(mid).webApi.post("gouvGouvernement/Create", {}, { 
                Nom: "Test ministère",
                Acronyme: "ACRO 123"
                }
        ).then(function (result) {
            alert("Ok");
        });
    }
</script>

and it works perfectly. It seems really to happend when I try to call from an Ajax form.


Solution

  • For the record, here is the final answer of the developer:

    When 2sxc calls a web api controller, it appends headers to the request which allow DNN and 2sxc to identify which portal, tab, module and app the request belongs to. This is needed because DNN needs to verify if the user has the corresponding rights to view the module, and 2sxc has to know in which context the api runs (for example, in which app to place data that should be created).

    But when calling a 2sxc api-controller from outside (no 2sxc context), the request headers are missing, so you need to specify them by hand. Instead of using http headers, you can add querystring parameters to the request.

    For your example, this would be the post url: http://mywebsite.com/api/2sxc/app/myapplication/api/mycontroller/Create?moduleid=123&tabid=12

    If other are using the api call, consider this bug when developing.