简介
在前一篇文章中,我们讨论了Razor页面。今天我们来谈谈处理方法(Handlers)。
我们知道可以将代码和模型放在 .cshtml 文件里面或与 .cshtml 匹配的 .cshtml.cs 文件中。
Razor页面处理程序或处理方法将用户请求匹配到我们的方法;请求来自 **.cshtml **文件。
Razor页面遵循特定的命名约定。从上一篇文章可以看出,.NET Core开发工具自动生成了很多处理方法,例如下面这些:
OnGetOnPostOnGetAsyncOnPostAsyncOnPostRemoveLoginAsyncOnGetLinkLoginCallbackAsyncetc..从列表中,我们可以看到这些名称遵循的具体模式。它们都是从On开始,随后Get或者Post,再其次是可选的 Haription.Contains(query)) .ToListAsync(); }
通过路由传递参数
以下是通过路由发送参数的两个示例:
<div> <form method="post" asp-page-handler="search" asp-route-query="Core"> <button>Search "Core"</button> </form> </div> <div> <form method="post" asp-page-handler="delete" asp-route-id="1"> <button>Delete ID 1</button> </form>hXGkOLSuQb; </div>
第一个是以前看到的search处理方法,它发送“Core”作为查询参数。
第二个是针对delete处理方法,并发送id为1,这表示它会删除第一条数据。
public async Task OnPostSearchAsync(string query) { Categories = await _dbContext .Categories .AsNoTracking() .Where(c => !string.IsNullOrEmpty(c.Description) && c.Description.Contains(query)) .ToListAsync(); } public async Task<IActionResult> OnPostDeleteAsync(int id) { var category = await _dbContext.Categories.FindAsync(id); if (category != null) { www.easck.com _dbContext.Categories.Remove(category); await _dbContext.SaveChangesAsync(); } return RedirectToPage(); }到此这篇关于ASP.NET Core中Razor页面的Handlers处理方法详解的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。








