[英]How to properly encode Web API parameter in URL path?
I have endpoint used to find book by book reference. 我有用于按书参考查找书的端点。
The book reference is string that can contain white-space and any kind of special characters, eg mybook
, my book
, my-book
, my/book
, book++
图书参考是可以包含空格和任何特殊字符的字符串,例如
mybook
, my book
, my-book
, my/book
, book++
// GET api/books/reference/{reference}
[HttpGet("reference/{reference}")]
public ActionResult<BookItem> FindByReference(string reference)
This is what I get when testing: 这是我在测试时得到的:
GET api/books/reference/mybook
OK
GET api/books/reference/my book
OK
GET api/books/reference/my-book
OK
GET api/books/reference/my+book
404 Not found
GET api/books/reference/my/book
404 Not found
GET api/books/reference/book++
404 Not found
What is a proper way to encode this reference
parameter IN THE URL PATH so that it gets properly resolved by routing? 什么是在URL路径中编码此
reference
参数以使其通过路由正确解析的正确方法? Is that even possible? 那有可能吗?
The encoding of the URLs is the responsibility of the client calling the API, a client must supply a valid URL if it wants a proper response. URL的编码是客户端调用API的责任,如果客户端需要适当的响应,则必须提供有效的URL。 If you encode your examples you will get:
如果对示例进行编码,您将获得:
GET api/books/reference/mybook
GET api/books/reference/my%20book
GET api/books/reference/my-book
GET api/books/reference/my%2Bbook
GET api/books/reference/my%2Fbook
GET api/books/reference/book%2B%2B
These should now work. 这些现在应该可以工作了。
If you want help with the actual encoding you will have to edit your question with the client source code. 如果您需要有关实际编码的帮助,则必须使用客户端源代码来编辑问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.