当 ID 包含句点时,ApiController 返回 404 [英] ApiController returns 404 when ID contains period
问题描述
我有一个 ApiController,我想使用电子邮件地址作为请求的 ID 参数:
I have an ApiController and I want to use email addresses as the ID parameter for requests:
// GET api/employees/email@address.com
public CompactEmployee Get(string id) {
var email = id;
return GetEmployeeByEmail(email);
}
但是,我无法让它工作(返回 404):
However, I cannot get this to work (returns 404):
http://localhost:1080/api/employees/employee@company.com
以下所有工作:
http://localhost:1080/api/employees/employee@company
http://localhost:1080/api/employees/employee@company.
http://localhost:1080/api/employees?id=employee@company.com
我在 web.config 中将 relaxedUrlToFileSystemMapping="true"
设置为 由 Phil Haack 详细介绍.
I have set relaxedUrlToFileSystemMapping="true"
in my web.config as detailed by Phil Haack.
我非常喜欢完整的电子邮件地址,但只要句号后跟任何其他字符,请求就会返回 404.任何帮助将不胜感激!
I would very much love the full email address to work, but any time the period is followed by any other character, the request returns a 404. Any help would be greatly appreciated!
由于缺乏其他选择,我朝着 Maggie 建议的方向前进并使用了这个问题的答案 创建重写规则,以便在 URL 中需要电子邮件时自动附加尾部斜杠.
Due to a lack of other options, I've headed in the direction Maggie suggested and used the answer from this question to create a rewrite rule to automatically append a trailing slash when I need an email in the URL.
<system.webServer>
....
<rewrite>
<rules>
<rule name="Add trailing slash" stopProcessing="true">
<match url="^(api/employees/.*.[a-z]{2,4})$" />
<action type="Rewrite" url="{R:1}/" />
</rule>
</rules>
</rewrite>
</system.webServer>
推荐答案
添加尾部斜杠是否适用于您的场景?
Would adding a trailing slash work for your scenario?
http://localhost:33021/api/employees/employee@company.com/
这篇关于当 ID 包含句点时,ApiController 返回 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!