Question

I´m struggling with URLs for ajax-reader/JSON. Each time I think I understand it, it seems that I haven´t. Please, can anybody explain the logic behind this???

I got this Controller:

 public class ServiceController : DnnApiController
    {
        [AllowAnonymous]
        [HttpGet]
        public HttpResponseMessage GetAllItems(int moduleId)
        {
            MyProjectController controller = new MyProjectController();
            IEnumerable<ItemInfo> items = controller.GetAllItems(moduleId);
            return Request.CreateResponse(HttpStatusCode.OK, items);
        }
    }

I got this Routemapper:

public class RouteMapper : IServiceRouteMapper
    {
        public void RegisterRoutes(IMapRoute mapRouteManager)
        {
            mapRouteManager.MapHttpRoute("MyProject",
                                         "default",
                                         "{controller}/{action}",
                                         new[] { "MyCompany.MyProject.Services" });
        }
    }

At what URL can I read the data with $.ajax() and what is the URL showing me the data in a browser?

Thanx in Advance!

Asle :)

Was it helpful?

Solution

This is how I do it (Note: this will only work with DNN6.2 and above);

  1. In the View.ascx.cs add

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ServicesFramework.Instance.RequestAjaxScriptSupport();
        ServicesFramework.Instance.RequestAjaxAntiForgerySupport();
        jQuery.RequestDnnPluginsRegistration();
    }
    

    This ensures that jquery and the required DNN ajax plugins are added.

  2. Initiate the services framework jquery plugin in the View.ascx like this inside javascript script tags (S.O. wouldn't allow me to include them)

var modId = <%=ModuleId %>;    
var sf = $.ServicesFramework(modId);
  1. Now in a separate javascript file or in the view.ascx control add the ajax function
function getAllItems(){
  $.ajax({
  type:"GET",
  url:sf.getServiceRoot("MyProject")+"Service/GetAllItems",
  beforeSend:sf.setModuleHeaders,
  data:{moduleId:modId},
  cache:false
  }).done(function(data){
    alert("Success!");
  }).fail(function(){
    alert("Crashed!");
  }).always(function(){
    //something you want done whether passed or failed
    //like hide progress bar, ajax spinner etc.
  });
}

The DNN jquery plugin will build the url which will look similar to this (Note: 142 is just for illustration purpose and will be replace with actual module id)

/DesktopModules/MyProject/API/Service/GetAllItems?moduleId=142

OTHER TIPS

The URL will be something like

/desktopmodules/SlidePresentation/API/SlidePresetnation.ashx/ListOfSlides

I have examples at

https://slidepresentation.codeplex.com/SourceControl/latest

but they were for DNN6, they might require a few updates due to the API changes for DNN 7

you can see a DNN7 module that has a service layer at https://dnnsimplearticle.codeplex.com/SourceControl/latest#cs/services/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top