url - 值为空字符串的 WebApi 请求 URL

标签 url asp.net-web-api space

我有一个数据库表的 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/

相关文章:

ASP.NET Web Api session 与静态变量

java扫描仪不接受两个单词之间有空格

java.net.URL 引用 CSS

php - 在 PHP 中获取服务器的主机名或 IP 地址

c# - 在 Asp.net Core Web API 中返回 "JsonResult/IActionResult"或 "Task<SomeObject>"或仅返回 "SomeObject"有什么区别?

asp.net - 为什么.Net WebApi 不自动检测请求内容类型并进行自动绑定(bind)?

linux - Linux中是否存在虚拟内存地址的复用?

css - div之间出现空格

javascript - 单击链接时页面向下滚动

java - 为什么 "+"添加到我在 url 生成器中的所有坐标?