InvalidOperationException:多个处理程序匹配。以下处理程序匹配路由数据并满足所有约束:
InvalidOperationException: Multiple handlers matched. The following handlers matched route data and had all constraints satisfied:
System.Threading.Tasks.Task OnGetAsync(),无效 OnGet()
我在 Microsoft Visual Studio 2017 中构建 .NET Core 2.1 应用程序时收到此错误。这是我认为存在错误的视图。它用于 index.cshtml razor 主页。
public class IndexModel : PageModel
{
private readonly AppDbContext _db;
public IndexModel(AppDbContext db)
{
_db = db;
}
public IList<Customer> Customers { get; private set; }
public async Task OnGetAsync()
{
Customers = await _db.Customers.AsNoTracking().ToListAsync();
}
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var contact = await _db.Customers.FindAsync(id);
if (contact != null)
{
_db.Customers.Remove(contact);
await _db.SaveChangesAsync();
}
return RedirectToPage();
}
public void OnGet()
{
}
}
Razor 页面使用基于约定的处理程序进行导航。
当前的 PageModel 有两个 Get 处理程序 Tasks.Task OnGetAsync()
和 Void OnGet()
,如异常中所述。
框架无法确定使用哪一个。
删除 void OnGet
,因为它似乎未被使用。
还建议检查 OnPostDeleteAsync
的命名,因为这也可能导致路由问题。
You can add handler methods for any HTTP verb. The most common
handlers are:
OnGet
to initialize state needed for the page. OnGet sample.
OnPost
to handle form submissions.
The Async
naming suffix is optional but is often used by convention
for asynchronous functions.
public class IndexModel : PageModel {
private readonly AppDbContext _db;
public IndexModel(AppDbContext db) {
_db = db;
}
public IList<Customer> Customers { get; private set; }
public async Task<IActionResult> OnGetAsync() {
Customers = await _db.Customers.AsNoTracking().ToListAsync();
return Page();
}
public async Task<IActionResult> OnPostAsync(int id) {
var contact = await _db.Customers.FindAsync(id);
if (contact != null) {
_db.Customers.Remove(contact);
await _db.SaveChangesAsync();
}
return RedirectToPage("/Index");
}
}
System.Threading.Tasks.Task OnGetAsync(),无效 OnGet()
public class IndexModel : PageModel
{
private readonly AppDbContext _db;
public IndexModel(AppDbContext db)
{
_db = db;
}
public IList<Customer> Customers { get; private set; }
public async Task OnGetAsync()
{
Customers = await _db.Customers.AsNoTracking().ToListAsync();
}
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var contact = await _db.Customers.FindAsync(id);
if (contact != null)
{
_db.Customers.Remove(contact);
await _db.SaveChangesAsync();
}
return RedirectToPage();
}
public void OnGet()
{
}
}
Razor 页面使用基于约定的处理程序进行导航。
当前的 PageModel 有两个 Get 处理程序 Tasks.Task OnGetAsync()
和 Void OnGet()
,如异常中所述。
框架无法确定使用哪一个。
删除 void OnGet
,因为它似乎未被使用。
还建议检查 OnPostDeleteAsync
的命名,因为这也可能导致路由问题。
You can add handler methods for any HTTP verb. The most common handlers are:
OnGet
to initialize state needed for the page. OnGet sample.OnPost
to handle form submissions.The
Async
naming suffix is optional but is often used by convention for asynchronous functions.
public class IndexModel : PageModel {
private readonly AppDbContext _db;
public IndexModel(AppDbContext db) {
_db = db;
}
public IList<Customer> Customers { get; private set; }
public async Task<IActionResult> OnGetAsync() {
Customers = await _db.Customers.AsNoTracking().ToListAsync();
return Page();
}
public async Task<IActionResult> OnPostAsync(int id) {
var contact = await _db.Customers.FindAsync(id);
if (contact != null) {
_db.Customers.Remove(contact);
await _db.SaveChangesAsync();
}
return RedirectToPage("/Index");
}
}