asp.net-mvc - 当 ID 包含句点时,ApiController 返回 404

标签 asp.net-mvc asp.net-web-api asp.net-mvc-routing

我有一个 ApiController,我想使用电子邮件地址作为请求的 ID 参数:

// GET api/employees/email@address.com
public CompactEmployee Get(string id) {
   var email = id;
   return GetEmployeeByEmail(email);
}

但是,我无法让它工作(返回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" 设置为 detailed by Phil Haack .

我非常希望完整的电子邮件地址能够正常工作,但只要句号后面跟着任何其他字符,请求就会返回 404。任何帮助将不胜感激!

解决方案

由于缺乏其他选择,我按照 Maggie 建议的方向前进,并使用 this question 中的答案。创建重写规则,以便在 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>

最佳答案

添加尾部斜杠适合您的场景吗?

http://localhost:33021/api/employees/employee@company.com/

关于asp.net-mvc - 当 ID 包含句点时,ApiController 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13298542/

相关文章:

asp.net - ASP.NET MVC5+ 中 "exists"路由约束的文档在哪里?

asp.net - 如何在我的自定义 MvcRouteHandler 中模拟 IgnoreRoute

c# - 自定义授权过滤器在 ASP.NET Core 3 中不起作用

C# 提取与 ID 关联的表的计数

asp.net - 跨请求检索和持久化状态

odata - 如何让 Odata Web API 以 XML 格式返回数据

angular - 如何从 Angular 4 中的 httppost 方法将整数值传递给 Controller ​​?

c# - 使用 Entity Framework 优化 LINQ 查询

c# - Azure 发布数据库第一个连接字符串不起作用

asp.net-mvc - MVC3 中 Url.RouteUrl() 和 Url.Action() 的区别