1
votes

I realy want your option. We update a .net MVC 3 project to MVC 4 with web api 2, this a web service. The ajax calls is from android machines with the Devextreme/Phonegap.

After the update the web service when i call login from android i get "No HTTP resource was found that matches the request URI 'http://www.whatever.gr/MVCService/Actions/Login'"

I want to set the route so that all methods of the MVC service can have parameters optional. I can't get this work..

We can't change the source on the android app,and ajax call is the code below

function doLogin(username, password, callback) {
    loading(true);

    var dataStr = "uid=" + uid + "&username=" + username + "&password=" + password;
    var success = false;
    var token = "";
    $.ajax({
        type: "POST",
        url: registrationServiceUrl + 'Login',
        data: dataStr,
        beforeSend: function () {
            //alert("registration no: " + registrationid);
        },
        success: function (retval) {
            if (retval.indexOf('SUCCESS') == 0) {
                success = true;
                token = retval.substring(8);
                //setTokenToSettings(token); 
            } else {
                success = false;
            };
            callback(success, token);
            loading(false);

        },
        error: function (retval) {
            callback(success, token);
            loading(false);

        }
    });
}

The route in MVC is

 public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services

                // Web API routes
                //config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

                config.Routes.MapHttpRoute(
                    name: "ActionApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }

When i hit like below with parameters in a browser i have a response 'http://www.whatever.gr/MVCService/Actions/Login?param1=1&param=2&param3=3'.

I want to have the behavior, when to hit 'http://www.whatever.gr/MVCService/Actions/Login' or 'http://www.whatever.gr/MVCService/Actions/Login?param1=1&param=2&param3=3&invalidParamName=x'.

And the web service method to be trigger with empty parameters values.

But without setting optional parameters in the method

public string Login(string pamar1 = "",string param2 = "", string param3 = "")

With MVC 3 this was working fine as I explained before.

Thanks in advance and for your time.

1
Hi, I don't think it's anything to do with your client application. I had the same problem a while ago. Please take a look at this post.stackoverflow.com/questions/19093603/simple-post-to-web-api/… and also see the reason behind it in this question stackoverflow.com/questions/19424493/…Amila
I have tried [HttpGet(), HttpPost()] public JsonResult<string> Login([FromBody]string uid, [FromBody]string username, [FromBody]string password) but i get An error has occurredDimitrios
I think this is the ajax call data: dataStr $.ajax({ type: "POST", url: registrationServiceUrl + 'Login', data: dataStr, but how this works on MVC 3??Dimitrios
You cannot use more than one [FormBody] attributeAmila

1 Answers

0
votes

In Web API, it's very simple to remember how parameter binding is happening.

  • if you POST simple types, Web API tries to bind it from the URL
  • if you POST complex type, Web API tries to bind it from the body of the request (this uses a media-type formatter).

This asp.net page explains it all. http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api