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