简体   繁体   English

Web API QueryString没有将第二个参数传递给控制器​​/操作

[英]Web API QueryString not passing second parameter to controller/action

I have a Web API application which is expected to return a list of clinical alerts for patients. 我有一个Web API应用程序,有望为患者返回临床警报列表。 The request is invoked with the following URL 使用以下URL调用请求

http://myserver:18030/api/Alerts/search?systemId=182&patientId=T000282L

where the systemId determines the clinical information system for which the patientID value is relevant. 其中systemId确定与PatientID值相关的临床信息系统。 The routing is set up as follows in WebApiConfig.cs 路由在WebApiConfig.cs中设置如下

public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
           name: "Alertsapi",
           routeTemplate: "api/Alerts/search",
           defaults: new { controller = "Alerts" , action = "search"}
       );

and the controller actions is as follows: 控制器动作如下:

    [ActionName("search")]
    public  List<Alert> GetAlerts(string systemId = "", string patientId = "")
    {
        var alerts = from a in db.Alerts
                    where a.alertAuthorReference.Equals(systemId)
                    where a.alertSubjectReference.Equals(patientId)
                    select a;
        return alerts.ToList();
    }

I was under the impression that QueryString parameters where automatically mapped to action method parameters, but in this example patientId is always null (or an empty string as I am supplying that as a default). 我给人的印象是QueryString参数自动映射到操作方法参数,但是在此示例中,PatientId始终为null(或默认为空字符串)。 I have tried reading the QueryString in code inside the action method, but it only has one member with key systemId. 我尝试读取action方法内代码中的QueryString,但是它只有一个成员,其键为systemId。

Why isn't the second parameter being passed through? 为什么不传递第二个参数?

I can work around it by using a QueryString of patientId=182:T000282L and then parsing this composite key, but I want to be able eventually to search on multiple parameters, and so may need to access a third or even fourth value from the query string. 我可以通过使用PatientId = 182:T000282L的QueryString然后解析此复合键来解决此问题,但是我希望最终能够搜索多个参数,因此可能需要从查询中访问第三个甚至第四个值串。

You need to define a route for that like 您需要为此定义一条路线

 routes.MapHttpRoute(
             name: "GetPagedData",
             routeTemplate: "api/{controller}/{action}/{pageSize}/{pageNumber}"
        )

your controller will be like 您的控制器将像

[HttpGet]
("GetPagedResult")]
HttpResponseMessage GetPagedResult(int StartIndex, int PageSize)
{
         // you can set default values for these parameters like StartIndex = 0 etc.
}

You can easily get what you need now with Web API 2 and Attribute Routing. 使用Web API 2和属性路由,您现在可以轻松获得所需的内容。

Take a look at this article: 看一下这篇文章:

http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

First you'll need to edit WebApiConfig.cs 首先,您需要编辑WebApiConfig.cs

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

[...] [...]

in your case you can test it works with this inside a controller: 在您的情况下,您可以在控制器中测试它是否可以与此一起工作:

    [Route("search")]
    [HttpGet]
    public string search(string systemId = "", string patientId = "")
    {

        return patientId;
    }

and to call it: 并称之为:

http://myserver:18030/search?systemId=182&patientId=T000282L

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

相关问题 Web API控制器-查询字符串中的&#39;action&#39;参数 - Web API Controller - 'action' parameter in query string MVC querystring参数未传递给操作 - MVC querystring parameter not passing through to action 将参数列表传递给Umbraco API插件控制器操作 - Passing parameter list to Umbraco API plugin controller action Web API 2 / MVC 5:属性路由将参数作为查询字符串传递,以在同一控制器上定位不同的操作 - Web API 2 / MVC 5 : Attribute Routing passing parameters as querystring to target different actions on same controller Web API 2控制器不接受QueryString参数 - Web API 2 controller does not accept QueryString parameters 使用不是 Web API 中的 id 的第二个参数路由到 controller - Routing to controller with a second parameter that's not an id in Web API Web API Controller中具有相同参数的多个HttpPost操作 - Multiple HttpPost action with the same parameter in Web API Controller 如何通过 URL 部分或查询字符串绑定 WebAPI controller 操作参数 - How to bind WebAPI controller action parameter either by URL part or querystring Web API路由两个控制器类似的操作,并且参数未在一个操作中填充参数值 - Web api routing two controller similar action and parameter not populating parameter value in one action 将带有&符号的参数传递给控制器​​操作方法 - Passing a parameter with an ampersand to a controller action method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM