Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an asp.net web api controller action (RESTful) that accepts 2 string parameters. Such a parameter can be empty. The web api action is consumed from AngularJS codes (client side Javascript) in a asp.net Razor view page.

The problem of that web api action is case 4 (see below) is never hit. In details, case 4 is supposed to run when paramter1 is passed with an empty string, and paramter2 is passed with a non-empty string. However, on running for this case and by using debugger, I find the value of paramter1 is bound to the value of parameter2, and the value of parameter2 becomes null or empty. So there is a wrong data binding with that web api action, and I do not know how to solve it. Please help. Thank you.

The web API controller action looks like:

        [HttpGet]
        [AllowAnonymous]
        public HttpResponseMessage GetProductByParamter1AndParameter2(string paramter1, string paramter2)
        {
            if (string.IsNullOrWhiteSpace(paramter1) && string.IsNullOrWhiteSpace(paramter2))
            {
                // case 1: do something 1 ...
            }
            else if (!string.IsNullOrWhiteSpace(paramter1) && !string.IsNullOrWhiteSpace(paramter2))
            {
                // case 2: do something 2 ...
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(paramter1) && string.IsNullOrWhiteSpace(paramter2))
                {
                    // case 3: do something 3 ...
                }
                else // when paramter1 is empty and paramter2 is not empty
                {
                    // case 4: do something 4 ... but this is never hit
            }
        }

And the custom route for that web API controller action looks like:

 config.Routes.MapHttpRoute(
                name: "ProductApi_GetProductByParamter1AndParameter2",
                routeTemplate: "api/ProductApi/GetProductByParamter1AndParameter2/{parameter1}/{parameter2}",
                defaults: new
                {
                    controller = "ProductApi",
                    action = "GetProductByParamter1AndParameter2",
                    parameter1 = "",
                    parameter2 = ""
                }
            );

In the cshtml view page, on the client side AngularJS (Javascript codes) to consume that web API, I am coding things like:

myApp.factory('ListProductFactory', function ($http, $q) {

        return {
            getProducts: function (par1, par2) {

                var url = _baseUrl + '/ProductApi/GetProductByParamter1AndParameter2/' + par1 + '/' + par2;
                return $http({
                    method: 'GET',
                    url: url
                })
            }
        };
    });
share|improve this question
Can you verify in your debugger that your parameters are coming in correctly? Or is that the issue, that some logic is preventing the scenario of p1=null p2=notNull? – MaxPRafferty Jul 29 at 15:09
@MaxPRafferty. Thanks for your reply. I debug the action with VS 2012 and set the break point at the open { bracket of the controller action, so I see the value bindings for the 2 parameters are wrong. As a result, case 4 never is hit. – Thomas.Benz Jul 29 at 19:46

2 Answers

On the controller you set the 'routeTemplate' as api/ProductApi/**GetSourceColumns**/{parameter1}/{parameter2}, it should be the name of the action GetProductByParamter1AndParameter2.

It doesn't even make sense that the url defined as something like: /ProductApi/GetProductByParamter1AndParameter2/{parameter1}/{parameter2} is still reaching the route that have been defined.

You probably have read these, but if you haven't, check out these links that explain key features of Web API

Model Validation: http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

Routing and Action Selection: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

share|improve this answer
Thanks for your pointing. That was my typo. I already corrected it. However, the problem persists. I will read those articles. – Thomas.Benz Jul 29 at 19:43
up vote 0 down vote accepted

To solve this problem, I use query-string approach instead of segment (/) approach:

var url = _baseUrl + '/ProductApi/GetProductByParamter1AndParameter2?parameter1=' + par1 + '&parameter2=' + par2;

I spent 10 days to figure out the answer for myself. It is painful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.