3

i am using fiddler to test my request..

I used below reuest to call my web api method ..it is working fine .

 http://localhost:50079/Import/Test/abc

Type :Get

web api method:

       [ActionName("Test")]
        public bool getconnection(string id)
        {
            return true;
        }

If i pass multi parameters i am getting error :HTTP/1.1 404 Not Found

I used like :

http://localhost:50079/Import/Test/abc/cde

 Type :Get

 web api method:

           [ActionName("Test")]
            public bool getconnection(string id,string value)
            {
                return true;
            }

I don't want to use any routes...Let me know why if i pass multi parameters why it is not recognized..

Kavitha
  • 1,447
  • 2
  • 22
  • 37
  • The general solution for both POST and GET requests I've answered [this question](http://stackoverflow.com/a/37298083/5714537). – Fatih GÜRDAL May 18 '16 at 11:29

4 Answers4

4

You have to specify a matching route

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

Try the above

TGH
  • 38,769
  • 12
  • 102
  • 135
2

You put the HttpGet attribute on the method, like this?

//http://localhost:50079/api/Import/abc?value=cde
[HttpGet]
[ActionName("Test")] 
public bool getconnection(string id,string value)   
{
    return true;   
}
TotPeRo
  • 6,561
  • 4
  • 47
  • 60
  • Tot , how can i pass value if my parameter has space b/w the word. like : Biz sight ( in URL) – Kavitha Feb 22 '14 at 07:25
  • The space character you need to replace with "%20" like this: http://localhost:50079/api/Import/abc?value=Biz%20sight – TotPeRo Feb 22 '14 at 07:29
1

TGH's answer is the more elegant solution.

However, if you don't want to use any routes, you will have to pass the additional parameters as query string parameters because the routing engine doesn't know which values to map to which variables (other than the id parameter configured in the default route).

Based on the Web API conventions, if you have a controller like this:

public class ImportController : ApiController
{
    [ActionName("Test")]
    public bool GetConnection(string id, string value)
    {
        return true;
    }
}

The corresponding URI will be:

http://localhost:50079/api/Import/abc?value=cde

If you want to map to use the [ActionName] attribute, you will need to configure the API to route by action name. See this tutorial.

Community
  • 1
  • 1
HTX9
  • 1,717
  • 3
  • 15
  • 27
  • i have passed http://localhost:50079/Import/Test/abc?value=cde from Fiddler....it is giving error..HTTP/1.1 404 Not Found – Kavitha Feb 22 '14 at 06:28
0

[FromBody] one parameter and [FromUri] one parameter. example:

public bool InserOrUpdate([FromBody] User user,[FromUri] IsNew)

[FromBody] => ajax data [FromUri] => QueryString data

But the solution is in this connection.

Community
  • 1
  • 1
Fatih GÜRDAL
  • 1,489
  • 1
  • 17
  • 19