了解 asp.net 核心端点路由的结构

Understanding the structure of an asp.net core endpoint route

以下路线中 :long 部分的用途是什么?

[HttpGet("api/{users:long}")]   
public async Task<IActionResult> Get([FromRoute] long userId)
{
    ...
}

我在 Controller 端点上看到过这个,但从未使用过它,那么它有什么作用?

那些被称为内联路由约束;你可以在这里查看关于它们的文章:https://mariusschulz.com/blog/inline-route-constraints-in-asp-net-core-mvc.

它让端点检查路由值确实是 long,否则它根本不会 select 到 运行 的那个动作。

它不应该用于验证 IMO,Marius 展示了一个可以使用这些约束的好例子:

public class MessagesController : ApiController
{
    [Route("messages/{messageId:int}")]
    public Message Get(int messageId)
    {
        // ...
    }

    [Route("messages/{messageId:guid}")]
    public Message Get(Guid messageId)
    {
        // ...
    }
}

根据路由值的类型,可以select编辑不同的操作。