简体   繁体   English

如何将多参数传递给web api

[英]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 . 我使用下面的reuest来调用我的web api方法..它工作正常。

 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 如果我传递多个参数我收到错误: 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? 你把HttpGet属性放在方法上,像这样?

//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. TGH的答案是更优雅的解决方案。

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). 但是,如果您不想使用任何路由,则必须将其他参数作为查询字符串参数传递,因为路由引擎不知道要将哪些值映射到哪些变量(默认情况下配置的id参数除外)路线)。

Based on the Web API conventions, if you have a controller like this: 基于Web API约定,如果您有这样的控制器:

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

The corresponding URI will be: 相应的URI将是:

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. 如果要映射以使用[ActionName]属性,则需要将API配置为按操作名称进行路由。 See this tutorial . 请参阅本教程

[FromBody] one parameter and [FromUri] one parameter. [FromBody]一个参数和[FromUri]一个参数。 example: 例:

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

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

But the solution is in this connection . 但解决方案就是这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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