WCF 4:GET请求传递空参数 [英] WCF 4: Passing Empty parameters on a GET request

查看:549
本文介绍了WCF 4:GET请求传递空参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建将只使用一个GET请求来从数据库返回一些搜索结果的API,我试图让这个可选参数可以传递(易与WCF),但也因此,如果参数只要它们是空的,他们将被服务忽略在查询字符串被specfie​​d。

不过,如果你有一个查询字符串参数为空,将服务器返回例如错误请求(400)

使用您所选择的最终用户的角度通过以下查询字符串

<$p$p><$c$c>http://www.exampleservice.com/basic/?apiKey=1234&noOfResults=3&maxSalary=&minSalary=&ouId=0&keywords=Web+Developer

注意 maxSalary和minSalary 不传递值

您那么有以下WCF服务:

  [OperationContract的]
[WebGet(UriTemplate = \"basic/?apiKey={apiKey}&noOfResults={noOfResults}&maxSalary={maxSalary}&minSalary={minSalary}&ouId={ouId}&keywords={keywords}\", BodyStyle = WebMessageBodyStyle.Bare)
        公开名单&LT; SearchResultsDto&GT; BasicSearch(字符串关键字,字符串apiKey,诠释noOfResults,诠释maxSalary,诠释minSalary,诠释OUID)
    {
          //做一些服务的东西
    }

这将导致400错误,请有人​​可以解释你是如何传递空参数对面一个WCF服务,或者这是不可能的?


解决方案

目前传递null或空参数不WCF的支持,解决这一问题的主要解决办法是重写它处理的URL querystringconverter因为它涉及通过排气管,但它达到经营合同之前。

implmenting的querystringconverter的延伸的一个很好的例子是在这里找到:


  

<一个href=\"http://stackoverflow.com/questions/354727/in-wcf-web-programming-model-how-can-one-write-a-operation-contract-with-an-arra\">In WCF Web编程模型中,一个人如何可以编写查询字符串参数数组(即具有相同的名称)一个经营合同?



可悲的是WCF 4中的错误,你不能覆盖querystringconverter,这已被微软解决,将其固定在SP1发布今年到来。

在那之前有没有干净的方式来处理这种情况以外来处理异常并返回400(错误的请求)的状态code - 的API应该在过渡处理这种良好的文档

I'm creating an API which will just use a get request to return some search results from the database, I'm trying to make it so that optional parameters can be passed (easy with WCF) but also so that if parameters are specfied in the query string as long as they are empty they will be ignored by the service.

However if you have the a query string with empty parameters it will return a bad request (400) by the server e.g.

Using a end-user point of your choice pass the following querystring

http://www.exampleservice.com/basic/?apiKey=1234&noOfResults=3&maxSalary=&minSalary=&ouId=0&keywords=Web+Developer

Note that maxSalary and minSalary are not passing values

You then have the following WCF service:

[OperationContract]
[WebGet(UriTemplate = "basic/?apiKey={apiKey}&noOfResults={noOfResults}&maxSalary={maxSalary}&minSalary={minSalary}&ouId={ouId}&keywords={keywords}", BodyStyle = WebMessageBodyStyle.Bare)]
        public List<SearchResultsDto> BasicSearch(string keywords, string apiKey, int noOfResults, int maxSalary, int minSalary, int ouId)
    {
          //Do some service stuff
    }

This will cause a 400 error, please can someone explain how you pass empty parameters across to a WCF service or is this just not possible?

解决方案

Currently passing null or an empty parameter is not supported in WCF, the main solution to this problem is to override the querystringconverter which handles the url as it comes through the pipe but before it reaches the operation contract.

An excellent example of implmenting an extension of the querystringconverter is found here:

In WCF web programming model, how can one write a operation contract with an array of query string parameters (i.e. with the same name)?

HOWEVER sadly there is a bug in WCF 4 where you cannot override the querystringconverter, this has been addressed by Microsoft and will be fixed in the SP1 release coming this year.

Until then there is no clean way to deal with this situation other than to handle the exception and return a status code of 400 (bad request) - good documentation of the api should handle this in the interim.

这篇关于WCF 4:GET请求传递空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆