【问题标题】:ASP .NET Core Web API route dot in url causes 404 not foundurl 中的 ASP .NET Core Web API 路由点导致 404 未找到
【发布时间】:2019-05-30 00:30:20
【问题描述】:

我在 ASP.NET Core Web api 中遇到 url 路由问题。当 URL 连续点 (.) 时,它返回 404 not found。例如,

http://localhost:9030/api/test/109/fake@email.com

不适用于电子邮件但不带点的代码

public class TestController : Controller{
{
    [HttpGet]
    [Route("{id:int}")]
    public IActionResult Get(int id)
    {
        //whatever
    }

    [HttpGet]
    [Route("{id:int}/{name:alpha}")]
    public IActionResult Get(int id, string name)
    {
        //whatever
    }
}

但是,这行得通

http://localhost:9030/api/test/getbyname/109/fake@email.com

public class TestController : Controller{
{
    [HttpGet("GetById/{id}")]
    public IActionResult Get(int id)
    {
        //whatever
    }

    [HttpGet("GetByName/{id}/{name}")]
    public IActionResult Get(int id, string name)
    {
        //whatever
    }
}

如何解决这个问题?

【问题讨论】:

  • fake@email.com 无论如何都是无效的。您是否忘记对@ 进行url 编码? urlencoder.org
  • @没问题。问题是点 (.)。我正在使用 IIS 8.5
  • 它可能工作,但你不应该这样传递@。你应该对它进行 url 编码。

标签: c# asp.net-core asp.net-core-webapi


【解决方案1】:

你的问题是

[Route("{id:int}/{name:alpha}")]

您说只接受字母字符,这意味着 a-z 和 A-Z 不包括任何特殊字符。您最好使用正则表达式来验证它是否始终是电子邮件地址。

试试这个

[Route("{id:int}/{name}")]

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多