ASP.NET Core 最小 API - 无法推断一个或多个参数
Posted
技术标签:
【中文标题】ASP.NET Core 最小 API - 无法推断一个或多个参数【英文标题】:ASP.NET Core minimal API - Failure to infer one or more parameters 【发布时间】:2022-01-05 04:26:09 【问题描述】:我在 Microsoft 文档上关注 this tutorial 以创建一个最小的 API,我在 Visual Studio 上遇到了这个错误:
System.InvalidOperationException : 'Failure to infer one or more parameters.
这里是我正在使用的代码:
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.MapGet("/", () => "Hello world !");
// CREATE NEW ITEM /INVENTORY/ITEM
app.MapPost("/inventory/item", async (Product product, ProductsDb db) =>
db.Products.Add(product);
await db.SaveChangesAsync();
return Results.Created($"/inventory/product.Barcode", product);
);
// DELETE ITEM /ITEM
app.MapDelete("/item/bc", async (int bc, ProductsDb db) =>
if (await db.Products.FindAsync(bc) is Product product)
db.Products.Remove(product);
await db.SaveChangesAsync();
return Results.Ok(product);
return Results.NotFound();
);
// UPDATE ITEM /INVENTORY/item/barecode
app.MapPut("/inventory/item/bc", async (int bc, Product inputProduct, ProductsDb db)
=>
var product = await db.Products.FindAsync(bc);
if (product is null) return Results.NotFound();
product.Name = inputProduct.Name;
product.Category = inputProduct.Category;
product.Price = inputProduct.Price;
product.Discount = inputProduct.Discount;
product.Quantity = inputProduct.Quantity;
await db.SaveChangesAsync();
return Results.NoContent();
);
// GET AL ITEMS /INVENTORY/ITEMS
app.MapGet("/inventory/item", async (ProductsDb db) =>
await db.Products.ToListAsync());
// GET AL ITEMS /INVENTORY/ITEMS/query?category=category
app.MapGet("/inventory/items/query?category=category", async (int bc, ProductsDb db)
=>
await db.Products.FindAsync(bc)
is Product product
? Results.Ok(product)
: Results.NotFound());
// GET AL ITEMS SORTED IN DESCENDING /INVENTORY/ITEM/SORT
app.MapGet("/inventory/item/sort", async (ProductsDb db) =>
await db.Products.OrderBy(p =>p.Price).ToListAsync());
app.Run();
class Product
public string Name get; set;
public string Category get; set;
public int Price get; set;
public int Discount get; set;
public int Quantity get; set;
public int Barcode get; set;
class ProductsDb : DbContext
public ProductsDb(DbContextOptions<ProductsDb> options)
: base(options)
public DbSet<Product> Products => Set<Product>();
我要做的是提供一些 API 端点,可以在数据库中获取或发布一些产品(实体框架代码优先)
我像教程一样使用 EntityFramework.InMemory,但我不知道它为什么会产生这个错误,我做错了什么吗?谢谢!
【问题讨论】:
【参考方案1】:首先,您需要在构建器之后添加上下文。
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add these two lines
builder.Services.AddDbContext<ProductsDb>(opt => opt.UseInMemoryDatabase("ProductsList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
接下来,您必须按如下方式更正此方法:
// GET AL ITEMS /inventory/items/category/barecode
app.MapGet("/inventory/items/category/bc", async (int bc, ProductsDb db)
// ...
最后,您必须在实体字符串中设置可为空值或默认值,以避免可空引用错误。
public string? Name get; set;
public string? Category get; set;
// OR
// public string Name get; set; = "default name";
// public string Category get; set; = "default category";
【讨论】:
以上是关于ASP.NET Core 最小 API - 无法推断一个或多个参数的主要内容,如果未能解决你的问题,请参考以下文章
创建API服务最小只要4行代码!!!尝新体验ASP.NET Core 6预览版本中的最小Web API(minimal APIS)新特性
ASP.NET Core Web API InvalidOperationException:无法解析服务 [重复]