Calling Web API Methods

Abhishek

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 }
        );
Yasser Shaikh

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Web api methods are not calling in Web performance tool Visual Studio

From Dev

calling https web service methods

From Dev

How to use SignalR 2 with WEB API, calling SignalR methods from API and from Clients

From Dev

Calling custom methods in API controller

From Dev

Calling google api explorer methods from a web application (java/jsp) endpoint

From Dev

"GetBy" Methods in Web API

From Dev

Calling a Web api from another Web api

From Dev

Calling web api from another web api

From Dev

web api controller calling issues

From Dev

Calling web api in c#

From Dev

Calling a web API via Swift

From Dev

Calling Web API with Async /Await

From Dev

calling smart contracts methods using web3 ethereum

From Java

Issues calling methods on web context objects in thymeleaf template

From Dev

How to rout to a web api when calling another web api

From Dev

Calling liferay portlet as web service with REST API

From Dev

Calling WEB API to download excel file

From Dev

No resource found error while calling WEB API

From Dev

Conditional ignore a property while calling Web API

From Dev

Calling a Web Api method when user is authenticated

From Dev

Web API calling aspx in the same solution

From Dev

Calling Web API from MVC controller

From Dev

Calling WEB API with basic authentication in C#

From Java

Java question (calling methods with parameters) using UnboundID LDAP SDK api

From Dev

How can the methods `makeConcat​` and `makeConcatWithConstants` in `StringConcatFactory` used by directly calling the API?

From Dev

Web api return values for async methods

From Dev

Web API methods never get routed to

From

Using WebAssembly to call Web API methods

From Dev

Web API Controller is not recognizing newly added methods

Related Related

  1. 1

    Web api methods are not calling in Web performance tool Visual Studio

  2. 2

    calling https web service methods

  3. 3

    How to use SignalR 2 with WEB API, calling SignalR methods from API and from Clients

  4. 4

    Calling custom methods in API controller

  5. 5

    Calling google api explorer methods from a web application (java/jsp) endpoint

  6. 6

    "GetBy" Methods in Web API

  7. 7

    Calling a Web api from another Web api

  8. 8

    Calling web api from another web api

  9. 9

    web api controller calling issues

  10. 10

    Calling web api in c#

  11. 11

    Calling a web API via Swift

  12. 12

    Calling Web API with Async /Await

  13. 13

    calling smart contracts methods using web3 ethereum

  14. 14

    Issues calling methods on web context objects in thymeleaf template

  15. 15

    How to rout to a web api when calling another web api

  16. 16

    Calling liferay portlet as web service with REST API

  17. 17

    Calling WEB API to download excel file

  18. 18

    No resource found error while calling WEB API

  19. 19

    Conditional ignore a property while calling Web API

  20. 20

    Calling a Web Api method when user is authenticated

  21. 21

    Web API calling aspx in the same solution

  22. 22

    Calling Web API from MVC controller

  23. 23

    Calling WEB API with basic authentication in C#

  24. 24

    Java question (calling methods with parameters) using UnboundID LDAP SDK api

  25. 25

    How can the methods `makeConcat​` and `makeConcatWithConstants` in `StringConcatFactory` used by directly calling the API?

  26. 26

    Web api return values for async methods

  27. 27

    Web API methods never get routed to

  28. 28

    Using WebAssembly to call Web API methods

  29. 29

    Web API Controller is not recognizing newly added methods

HotTag

Archive