1

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) {

    });
};
VDWWD
  • 35,079
  • 22
  • 62
  • 79
John Kane
  • 4,383
  • 1
  • 24
  • 42

1 Answers1

2

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" });
Fix It Scotty
  • 2,852
  • 11
  • 12
  • sorry it took me so long to respond, I had a couple of other tasks that I had to complete before coming back to this. It turns out that my issue was caused by a residual module that I had thought I removed by reverting my database. Which didnt remove/change the files that the module modified. I fixed that issue and now I am getting a 401 (unauthorized) any ideas what could case that based on my configuration as displayed in the question? – John Kane Aug 22 '18 at 19:04
  • @JohnKane - comment out the SupportedModules and DnnModuleAuthorize attributes on your ItemController class and add [AllowAnonymous]. Then check if you get the proper response. If so, comment out allowanonymous and uncomment the other attributes one at a time. If the problem is the SupportedModules attribute, try changing the value for in the manifest file to also be "ImportantDatesModule". I like to ensure that my moduleName (under component type=Module) in the manifest is the same as the "moduleFolderName" in the route. – Fix It Scotty Aug 22 '18 at 20:21
  • I commented out the supported modules and dnn module authorize and added in the AllowAnonymous and I am still seeing a 401 error... it seems really odd. Does it make a difference if you put the allow anonymous at the class or method level? – John Kane Aug 22 '18 at 20:52
  • The AllowAnonymous on the class level should apply to all methods on that class unless another more restrictive attribute is added to the method. I'm not sure why you are getting a 401 still. Maybe try installing the module on another environment. – Fix It Scotty Aug 23 '18 at 03:33
  • sorry, it turns out in my haste to get this working, that I placed the AllowAnonymous on the wrong controller... I am getting a 200 now that I added it into the correct controller. I tried uncommenting both the supported modules and dnn module authorize one at a time and it gives a 404 again no matter which was uncommented (this was uncommenting them one at a time) – John Kane Aug 23 '18 at 13:02
  • I found the issue... I had mixed up two parameters in my javascript code thank you for your help – John Kane Aug 23 '18 at 14:49
  • @JohnKane - Awesome! Glad you figure it out. – Fix It Scotty Aug 23 '18 at 16:09