我有一个数据库表的 WebApi 端点,其中 3 列是 PK。 在某些情况下,其中一个键是数据库中的空字符串 ('')。
是否可以在我的请求 URL 中提供此空字符串值?
示例:
[RoutePrefix("api/Endpoint")]
public partial class APIUserCustomerController : BaseApiController<APIUserCustomer>
{
[HttpDelete, Route("{UserName}/{CustomerNum}/{ShippingAddress}")]
public HttpResponseMessage Delete(System.String UserName, System.String CustomerNum, System.String ShippingAddress)
{
...
}
}
我已经尝试过:
DELETE api/Endpoint/username/customerNum/
,这给了我HTTP错误404.0 - 未找到
以及:
DELETE api/Endpoint/username/customerNum/%20
这给了我“/”应用程序中的服务器错误。找不到资源。
是否有我缺少的解决方案,或者我只是对数据库中的空字符串感到满意?
最佳答案
在optional parameters中找到了解决方案:
[RoutePrefix("api/Endpoint")]
public partial class APIUserCustomerController : BaseApiController<APIUserCustomer>
{
[HttpDelete, Route("{UserName}/{CustomerNum}/{ShippingAddress?}")]
public HttpResponseMessage Delete(System.String UserName, System.String CustomerNum, System.String ShippingAddress = "")
{
...
}
}
现在我可以打电话
如果我有发货地址,请删除 api/Endpoint/username/customerNum/shippingAddress
或
如果发货地址为空,则删除 api/Endpoint/username/customerNum
。
效果很好!!
关于url - 值为空字符串的 WebApi 请求 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25294811/