0

I am trying to call the DataByLocation with parameter but its calling DataByLocation function with no parameter. Am I missing something? Below is the code. Thanks in advance.

Js Code

 getData:function(){
        var _data = {_location:'ABC'};
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: 'ABService/api/ABService/DataByLocation',
            data: JSON.stringify(_data),
            success: this.receivedData                
        });
    }

Controller Code

[HttpPost]
public string DataByLocation(string _location)
{
    return _location;
}
[HttpPost]
public string DataByLocation()
{

    return "no parameter";
}

Config Code

    RouteTable.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional }
        );
Abhishek
  • 196
  • 11
  • Do you mean to be setting `contentType`? Or should you be setting `dataType`? – jfriend00 Oct 16 '14 at 05:33
  • @jfriend00 , i think `contentType` is fine. http://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object – Arindam Nayak Oct 16 '14 at 05:38
  • ya as its working for parameter less function. – Abhishek Oct 16 '14 at 05:39
  • If you set `dataType` to JSON, then you wouldn't have to stringify the data yourself (jQuery would do it for you), but it probably can work either way since you've already manually turned it into JSON. – jfriend00 Oct 16 '14 at 05:40

2 Answers2

1

Your route api/{controller}/{id} specifies the parameter name to be "id".

So say you have a Web Api Controller like one below

public class AbcApiController : ApiController
{
    public Something Get(string searchTerm)
    {
        return ...;
    }
}

and if you try to access /api/AbcApi/get/hello where hello is the search term you pass. It wont work because this route will search of Get action with a parameter id.

Instead what will work for you, if you do not want to change your route is this

/api/AbcApi/get?searchTerm=hello

Plus if you are interested in learning more about web api routing, I would recommend you to read this post - Web Api Routing for multiple Get methods in ASP.NET MVC 4

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • I am getting " Could not load file or assembly 'System.Web.Http, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) " first need to get rid of this. – Abhishek Oct 16 '14 at 05:44
  • Are you getting this now with this solution or from the start ? – Yasser Shaikh Oct 16 '14 at 05:46
  • its resolved by [HttpGet] and assembly issue resolve by installing latest webapi through nuget. – Abhishek Oct 16 '14 at 06:31
0

Since you have not posted actual class name, i am assuming this to be TestController. Keeping that in mind, you can use following URLs for webapi. Assume , this is hosted in http://www.example.com`

If you need execute without id , then url will be.

http://www.example.com/api/Test/DataByLocation

If with id then url will remain same, but you need to pass post object as JSON.

EDIT

Since controller name is ABService , and virtual directory is ABService

URL will be http://www.example.com/ABService/api/ABService/DataByLocation

SOURCE

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • thanks of the reply, but the controller name in this case is ABServiceController. – Abhishek Oct 16 '14 at 05:33
  • actually the name of virtual diectory is ABService thats the url is like 'ABService/api/ABService/DataByLocation', but now ended up with error 'Could not load file or assembly 'System.Web.Http, Version=5.2.0.0,.. ' – Abhishek Oct 16 '14 at 05:38