AspNetCore Odata 8.0 获取单个实体404未找到
AspNetCore Odata 8.0 Get single entity 404 not found
我最近开始使用 Microsoft.AspNetCore.OData(8.0.0 版)包,在控制器中实现检索单个实体时遇到问题,returns 404 Not found 响应时我尝试请求 URL。以下是我的代码和配置,感谢您的查看:
套餐
- Microsoft.AspNetCore.OData(8.0.0)
- Microsoft.EntityFrameworkCore(6.0.2)
- Microsoft.EntityFrameworkCore.SqlServer(6.0.2)
- Microsoft.EntityFrameworkCore.工具(6.0.2)
- Microsoft.Extensions.DependencyInjection(6.0.0)
- Microsoft.Extensions.Logging(6.0.0)
Targeting framework: .NET 6.0
Project: ASP.NET Core Web API
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<DbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DbContext")));
// Add services to the container with odata
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<User>("users");
builder.Services.AddControllers().AddOData(options =>options.AddRouteComponents("v1",odataBuilder.GetEdmModel()).Filter().Select().OrderBy().SetMaxTop(100).SkipToken().Count());
User.cs
[Table("User")]
public class User
{
[Required]
[Key]
[MaxLength(256)]
[Column("Id")]
public string Id { get; set; }
[Required]
[MaxLength(256)]
[Column("Surname_en")]
public string Surname_en { get; set; }
[Required]
[MaxLength(256)]
[Column("Firstname_en")]
public string Firstname_en { get; set; }
[MaxLength(50)]
[Column("Surname_cn")]
public string Surname_cn { get; set; }
[MaxLength(50)]
[Column("Firstname_cn")]
public string Firstname_cn { get; set; }
[MaxLength(1024)]
[Column("Email")]
public string? Email { get; set; }
[Required]
[Column("CreatedDate")]
public DateTime CreatedDate { get; set; }
}
UsersController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Formatter;
public class UsersController : ODataController
{
private readonly DbContext _context;
public UsersController(DbContext context)
{
_context = context;
}
[EnableQuery(PageSize = 50)]
public IQueryable<User> Get()
{
return _context.User.AsQueryable();
}
[EnableQuery]
public SingleResult<User> Get([FromODataUri] string id)
{
return SingleResult.Create(_context.User
.Where(m => m.Id == id));
}
}
第一个 Get 函数 正在 与 URL:
https://localhost/v1/users
但是第二个函数 不工作 URL,它 returns 一个 404 Not found 状态代码,没有错误消息,它确实根本没有命中函数(当我在代码行设置断点时):
https://localhost/v1/users(test@123.com)
或
https://localhost/v1/users('test@123.com')
或
https://localhost/v1/users/test@123.com
经过多次尝试,包括更改NuGet打包版本、添加和删除属性、使用其他类型的变量作为主键...等等。我发现解决方案与配置、数据类型和属性无关。是参数的名称!
参数名必须写成Key
[EnableQuery]
public SingleResult<User> Get([FromODataUri]Guid key)
{
return SingleResult.Create(_context.User
.Where(m => m.Id == key));
}
现在已经很好用了,参数名是Key,不管我把类型改成Guid。
我最近开始使用 Microsoft.AspNetCore.OData(8.0.0 版)包,在控制器中实现检索单个实体时遇到问题,returns 404 Not found 响应时我尝试请求 URL。以下是我的代码和配置,感谢您的查看:
套餐
- Microsoft.AspNetCore.OData(8.0.0)
- Microsoft.EntityFrameworkCore(6.0.2)
- Microsoft.EntityFrameworkCore.SqlServer(6.0.2)
- Microsoft.EntityFrameworkCore.工具(6.0.2)
- Microsoft.Extensions.DependencyInjection(6.0.0)
- Microsoft.Extensions.Logging(6.0.0)
Targeting framework: .NET 6.0
Project: ASP.NET Core Web API
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<DbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DbContext")));
// Add services to the container with odata
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<User>("users");
builder.Services.AddControllers().AddOData(options =>options.AddRouteComponents("v1",odataBuilder.GetEdmModel()).Filter().Select().OrderBy().SetMaxTop(100).SkipToken().Count());
User.cs
[Table("User")]
public class User
{
[Required]
[Key]
[MaxLength(256)]
[Column("Id")]
public string Id { get; set; }
[Required]
[MaxLength(256)]
[Column("Surname_en")]
public string Surname_en { get; set; }
[Required]
[MaxLength(256)]
[Column("Firstname_en")]
public string Firstname_en { get; set; }
[MaxLength(50)]
[Column("Surname_cn")]
public string Surname_cn { get; set; }
[MaxLength(50)]
[Column("Firstname_cn")]
public string Firstname_cn { get; set; }
[MaxLength(1024)]
[Column("Email")]
public string? Email { get; set; }
[Required]
[Column("CreatedDate")]
public DateTime CreatedDate { get; set; }
}
UsersController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Formatter;
public class UsersController : ODataController
{
private readonly DbContext _context;
public UsersController(DbContext context)
{
_context = context;
}
[EnableQuery(PageSize = 50)]
public IQueryable<User> Get()
{
return _context.User.AsQueryable();
}
[EnableQuery]
public SingleResult<User> Get([FromODataUri] string id)
{
return SingleResult.Create(_context.User
.Where(m => m.Id == id));
}
}
第一个 Get 函数 正在 与 URL:
https://localhost/v1/users
但是第二个函数 不工作 URL,它 returns 一个 404 Not found 状态代码,没有错误消息,它确实根本没有命中函数(当我在代码行设置断点时):
https://localhost/v1/users(test@123.com)
或
https://localhost/v1/users('test@123.com')
或
https://localhost/v1/users/test@123.com
经过多次尝试,包括更改NuGet打包版本、添加和删除属性、使用其他类型的变量作为主键...等等。我发现解决方案与配置、数据类型和属性无关。是参数的名称!
参数名必须写成Key
[EnableQuery]
public SingleResult<User> Get([FromODataUri]Guid key)
{
return SingleResult.Create(_context.User
.Where(m => m.Id == key));
}
现在已经很好用了,参数名是Key,不管我把类型改成Guid。