Forum


HomeHomePremiumPremiumDevelopmentDevelopmentangular $http.get with querystring parameters calling DNN web api issueangular $http.get with querystring parameters calling DNN web api issue
Previous
 
Next
New Post
9/10/2016 1:15 AM
 

I just finished viewing the DNN advanced module development course (and prerequisites) and am trying to grasp everything.

While testing I can't seem to get an $http.get request working with additional parameters. The angular module code (00.00.02) has the "byid" example on the ItemController but this service never gets called from the client side so there is actually no example of GET with parameters. In my controller class the parameter object is always null.

Here are some snippets of what I've tried:

[HttpGet]
        [ValidateAntiForgeryToken]
        [ActionName("test")]
        [DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]

        public HttpResponseMessage GetTestByModuleId(ParamObject parameters)
        {
            try
            {
                var test = parameters.TestValue; // > the parameters object is always null so we fail here with an object not referenced to an instance of an object exception
                ...

The ParamObject:

public class ParamObject
    {
        public string TestValue { get; set; }
    }

The Client side code:

$http.get(serviceRoot + 'slide/test', { params: { TestValue: "testvalue" } })
            .then(function(response) {
                    vm.TestResult = response.data;
                },
                function(errorPayload) {
                    $log.error('Loading of testdata failed', errorPayload);
                });

 

I have also tried some variations by using the [FromUri] attribute or by passing it in as a string and serializing and deserializing it but so far no success... Do you have an idea?

 

 
New Post
9/10/2016 5:21 AM
 

Ok, I added an extra route to my ServiceRouteMapper. After adding an extra route with {controller}/{action}/{testValue} it now accepts the parameter object that is passed in the url, if I use a simple type in the web api method, so if I need to add 2 parameters I need to add another route to support that.

 

Is it also possible to only add a route for one parameter and accept the params object from angular which may contain multiple variables inside a custom object in c# like I was trying to do in the first place and like the POST examples in the tutorial do..?

 
New Post
9/13/2016 6:49 AM
 

Hey Steven. In my experience if I need to pass multiple parameters I use POST not GET. It's easy to create a model in c# to mirror the expected passed result from the front end. However Here's a simple example where I pass a single parameter to a GET.

mapRouteManager.MapHttpRoute(
                "Module",
                "default",
                "{controller}/{action}",
                new[] { "Module.Api.Controllers" }
            );

Here's my controller code http header:

[HttpGet]
        public HttpResponseMessage Details(string id)

And here's my javascript:

DnnApiService.get('ClaimDetail/ClaimDetails', { claimId: $scope.claimDetail.claimId }, function(success) {}, function(fail) {});

I have a reusable DnnApiService that handles all the request headers so my request will look different but it functions the same. Hope that helps a little.

 
New Post
9/15/2016 2:26 AM
 

Thanks for the response Michael. If I have only the {controller}/{action} route and pass in a parameter by either using the params object or just passing it in like you did, I get an error message: "No HTTP resource was found that matches the request URI 'http://dnndev.me/DesktopModules/ContentSlider/API/slide/test1'." and "No action was found on the controller 'Slide' that matches the request."

I'll experiment some more and probably also use POST when needing to pass multiple parameters.

    1. Message:"No HTTP resource was found that matches the request URI 'http://dnndev.me/DesktopModules/ContentSlider/API/slide/test1'."
    2. MessageDetail:"No action was found on the controller 'Slide' that matches the request."
    1. Message:"No HTTP resource was found that matches the request URI 'http://dnndev.me/DesktopModules/ContentSlider/API/slide/test1'."
    2. MessageDetail:"No action was found on the controller 'Slide' that matches the request."
    1. Message:"No HTTP resource was found that matches the request URI 'http://dnndev.me/DesktopModules/ContentSlider/API/slide/test1'."
    2. MessageDetail:"No action was found on the controller 'Slide' that matches the request."
    1. Message:"No HTTP resource was found that matches the request URI 'http://dnndev.me/DesktopModules/ContentSlider/API/slide/test1'."
    2. MessageDetail:"No action was found on the controller 'Slide' that matches the request."
 
Previous
 
Next
HomeHomePremiumPremiumDevelopmentDevelopmentangular $http.get with querystring parameters calling DNN web api issueangular $http.get with querystring parameters calling DNN web api issue



Try FREE
30 days money back guaranteed