简体   繁体   中英

how to pass mult parameters to web api

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..

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

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;   
}

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 .

[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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM