dotnetnukedotnetnuke-9

Routing in DNN SPA Module is not working as expected


I cannot seem to figure out how to access the methods on one of my controllers, the Settings controller works without issue...

ServiceRouteMapper:

public void RegisterRoutes(IMapRoute mapRouteManager)
{
       mapRouteManager.MapHttpRoute(
           moduleFolderName: "ImportantDatesModule",
           routeName: "default",
           url: "{controller}/{action}/{id}",
           defaults: new { id = RouteParameter.Optional },
           namespaces: new[] { "company.ImportantDatesModule.Services" });
}

Controller:

[SupportedModules("ImportantDatesModule")]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
public class ItemController : DnnApiController
...

[HttpGet]
[ActionName("getItems")]
public HttpResponseMessage GetItems() 
{
    return Request.CreateResponse(HttpStatusCode.OK, "success");
}

JS file which is doing the call:

dnnspamodule.itemsViewModel = function (moduleId, resx) {
var service = {
    path: "ImportantDatesModule",
    framework: $.ServicesFramework(moduleId),
    controller: "Item"
}
service.baseUrl = service.framework.getServiceRoot(service.path);
...

var getItems = function () {
    var restUrl = service.baseUrl + service.controller + "/getItems";
    console.log(restUrl);

    isLoading(true);
    var jqXHR = $.ajax({
        url: restUrl,
        beforeSend: service.framework.setModuleHeaders,
        dataType: "json"
    }).done(function (data) {
        if (data) {
            load(data);
            isLoading(false);
        }
        else {
            // No data to load 
            itemList.removeAll();
        }
    }).always(function (data) {

    });
};

Solution

  • I don't know what your Settings controller looks like, but maybe try separating your routes into action style ({controller}/{action}) vs rest-style routes ({controller}/{id} + derived verb in the method name). Try this in your service route mapper:

    mapRouteManager.MapHttpRoute(
        moduleFolderName: "ImportantDatesModule",
        routeName: "default", 
        url: "{controller}/{action}",
        namespaces: new[] {"company.ImportantDatesModule.Services"});
    
    mapRouteManager.MapHttpRoute(
        moduleFolderName: "ImportantDatesModule",
        routeName: "rest",
        url: "{controller}/{id}",
        defaults: new { id = RouteParameter.Optional },
        namespaces: new[] { "company.ImportantDatesModule.Services" });