网站建设中单页面汕头网络公司网站建设
网站建设中单页面,汕头网络公司网站建设,做设计需要素材的常用网站,全国最好设计培训简介
在前一篇文章中#xff0c;我们讨论了Razor页面。今天我们来谈谈处理方法#xff08;Handlers#xff09;。我们知道可以将代码和模型放在 .cshtml 文件里面或与 .cshtml 匹配的 .cshtml.cs 文件中。Razor页面处理程序或处理方法将用户请求匹配到我们的方法#xff1…简介
在前一篇文章中我们讨论了Razor页面。今天我们来谈谈处理方法Handlers。我们知道可以将代码和模型放在 .cshtml 文件里面或与 .cshtml 匹配的 .cshtml.cs 文件中。Razor页面处理程序或处理方法将用户请求匹配到我们的方法请求来自 .cshtml 文件。Razor页面遵循特定的命名约定。从上一篇文章可以看出.NET Core开发工具自动生成了很多处理方法例如下面这些
OnGetOnPostOnGetAsyncOnPostAsyncOnPostRemoveLoginAsyncOnGetLinkLoginCallbackAsyncetc..
从列表中我们可以看到这些名称遵循的具体模式。它们都是从On开始随后Get 或者Post再其次是可选的 Handler名称 RemoveLoginLinkLoginCallback最后Async后缀为 异步 方法。
示例项目可在GitHub上找到需要使用最新的.NET Core 2.0.0 CLI。
默认POST和GET处理方法
打开页面将在代码背后触发默认的Get或GetAsync处理方法类似地提交表单将触发默认Post或PostAsync处理方法 form methodPOSTdivName: input asp-forCategory.Name //divdivDescription: input asp-forCategory.Description //divbutton typesubmit classbtn btn-primarySave/button/form
触发的方法 public async TaskIActionResult OnPostAsync() { if (!ModelState.IsValid){ return Page();}_dbContext.Categories.Add(Category); await _dbContext.SaveChangesAsync(); return RedirectToPage(./Index);}
使用OnPostAsync或OnPost为处理方法名称都可以正常工作。如果您使用的是OnPost那么代码中不能使用异步调用。
但是如果您同时实现两种OnPostAsync和OnPOST等处理方法您会遇到这样的问题 自定义处理方法名称
除了默认的处理方法名称我们还可以指定自定义名称。
在 .cshtml 文件中的实现以下代码 form methodPOSTdivDescription: input asp-forCategory.Description //divinput typesubmit valueSave First asp-page-handlerFirst classbtn btn-primary btn-xs //form
这会创建一个包含Description字段的简单表单 在Razor页面中将表单处理方法添加到匹配的 .cshtml.cs 文件代码文件方法命名为OnPostFirst 或 OnPostFirstAsync 具体取决于要在其中运行的代码类型。假设我们需要在数据库中插入Category并保存这些更改使用Entity Framework的异步方法 public async TaskIActionResult OnPostFirstAsync() {Category.Name First;_dbContext.Categories.Add(Category); await _dbContext.SaveChangesAsync(); return RedirectToPage(./Categories/Index);}
请注意名称 OnPost First Async 。
同一页面多个POST处理方法
让我们扩展刚才这一段代码添加POST方法另一种形式
下面是 .cshtml 的代码 form methodPOSTdivDescription: input asp-forCategory.Description //divinput typesubmit valueSave First asp-page-handlerFirst classbtn btn-primary btn-xs //formform methodPOSTdivDescription: input asp-forCategory.Description //divinput typesubmit valueSave Second asp-page-handlerSecond classbtn btn-primary btn-xs //form
这两个表单将分别匹配代码中这两种方法 public async TaskIActionResult OnPostFirstAsync() { return await InsertCatepory(First);} public async TaskIActionResult OnPostSecondAsync() { return await InsertCatepory(Second);} private async TaskIActionResult InsertCatepory(string name) {Category.Name name;_dbContext.Categories.Add(Category); await _dbContext.SaveChangesAsync(); return RedirectToPage(./Categories/Index);}
关键的代码是使用 asp-page-handler Tag Helper指定表单的处理方法的名称。
我们也可以在一个表单通过两个提交按钮实现同样的事情 form methodPOSTdivDescription: input asp-forCategory.Description //divinput typesubmit valueSave First asp-page-handlerFirst classbtn btn-primary btn-xs /input typesubmit valueSave Second asp-page-handlerSecond classbtn btn-primary btn-xs //form
处理方法参数
将参数传递给处理方法有两种方法
表单输入表单元素借助 asp-route Tag Helper
通过表单输入传递参数
对于表单输入作为输入参数名称必须是同步的。HTML input元素的名称必须与处理方法参数的名称相匹配: form methodPOSTinput typetext namequery/button typesubmit asp-page-handlersearchSearch/button/form public async Task OnPostSearchAsync(string query) {Categories await _dbContext.Categories.AsNoTracing().Where(c !string.IsNullOrEmpty(c.Description) c.Description.Contains(query)).ToListAsync();}
通过路由传递参数
以下是通过路由发送参数的两个示例 divform methodpost asp-page-handlersearch asp-route-queryCore buttonSearch Core/button/form/divdivform methodpost asp-page-handlerdelete asp-route-id1buttonDelete ID 1/button/form/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 TaskIActionResult OnPostDeleteAsync(int id) { var category await _dbContext.Categories.FindAsync(id); if (category ! null) {_dbContext.Categories.Remove(category); await _dbContext.SaveChangesAsync();} return RedirectToPage();}
相关文章
asp.net core mvc实现伪静态功能如何在多个项目中分离Asp.Net Core Mvc的Controller和Areasasp.net core 编译mvcroutingsecurity源代码进行本地调试ASP.NET Core MVC四种枚举绑定方式ASP.NET Core MVC 模型绑定用法及原理ASP.NET Core MVC 控制器创建与依赖注入ASP.NET Core MVC 过滤器介绍ASP.NET Core MVC Tag Helpers 介绍ASP.NET Core MVC – Caching Tag HelpersASP.NET Core MVC – Form Tag HelpersASP.NET Core MVC – 自定义 Tag HelpersASP.NET Core - Razor 页面介绍
原文地址http://www.cnblogs.com/tdfblog/p/razor-pages-handlers-in-asp-net-core.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88503.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!