c# - OData 路径模板不是有效的 OData 路径模板

标签 c# rest asp.net-web-api2 odata

我有一个具有 ODataRoute 的 HttpGet 方法

["Users({userId}/Tags)"]

userId 是一个字符串,方法名是UserTags。 Controller 是 UsersController

当我运行该应用程序时,出现以下错误:

The path template Users({userId})/Tags on the action 'UserTags' in controller Users is not a valid OData path template. Found an unresolved path segment Tags in the OData path template Users({userId})/Tags.

最佳答案

ODataRoute 的约束非常严格,您的用户实体必须有一个名为“Tags”的集合属性才能使您的路由生效。

使用下面的代码,我让它正常工作:

public class UserController : ODataController
{
    [HttpGet]
    [System.Web.OData.Routing.ODataRoute("User({userId})/Tags")]
    public IHttpActionResult GetTags([FromODataUri]int userId)
    {
        //...
    }
}

public class User
{
    [Key]
    public int Id { get; set; }
    public List<Tag> Tags { get; set; }
}

关于c# - OData 路径模板不是有效的 OData 路径模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49319111/

相关文章:

c# - 是否可以在 iPhone 和 OS X 上不使用 NIB 文件来创建窗口?

c# - 如何在 .NET Core 中使用 HttpWebResponse 和 WebResponse?

c# - LINQ:SubmitChanges 上的多个 DeleteOnSubmit 订单行为

c# - 使用 DataContractJsonSerializer,将 JSON 字符串反序列化为具有列表和接口(interface)作为属性的 C# 对象

java - Spring Boot REST API 接受所有请求并返回空白响应

exception-handling - IExceptionLogger 是否不赞成在 Web API 2 中对 ExceptionFilterAttribute 的需求?

c# - Store 没有实现 IUserPasswordStore<TUser> ASP.NET Identity

c# - 使用 iTextSharp 将图像转换为 PDF 保留剪切路径

python - Django Rest Framework 将类级别方法添加到 api

c# - WebClient 与 HttpClient - 异步请求