当前位置: 代码网 > it编程>编程语言>Asp.net > ASP.NET Core中Razor页面的Handlers处理方法详解

ASP.NET Core中Razor页面的Handlers处理方法详解

2024年05月19日 Asp.net 我要评论
简介在中,我们讨论了razor页面。今天我们来谈谈处理方法(handlers)。我们知道可以将代码和模型放在.cshtml文件里面或与.cshtml匹配的.cshtml.cs文件中。razor页面处理

简介

在中,我们讨论了razor页面。今天我们来谈谈处理方法(handlers)。

我们知道可以将代码和模型放在 .cshtml 文件里面或与 .cshtml 匹配的 .cshtml.cs 文件中。

razor页面处理程序或处理方法将用户请求匹配到我们的方法;请求来自 **.cshtml **文件。

razor页面遵循特定的命名约定。从上一篇文章可以看出,.net core开发工具自动生成了很多处理方法,例如下面这些:

  • onget
  • onpost
  • ongetasync
  • onpostasync
  • onpostremoveloginasync
  • ongetlinklogincallbackasync
  • etc..

从列表中,我们可以看到这些名称遵循的具体模式。它们都是从on开始,随后get或者post,再其次是可选的 handler名称(removelogin,linklogincallback),最后async后缀为异步方法。

示例项目可在github上找到,需要使用最新的.net core 2.0.0 cli

默认post和get处理方法

打开页面将在代码背后触发默认的getgetasync处理方法;类似地,提交表单将触发默认postpostasync处理方法:

    <form method="post">
        <div>name: <input asp-for="category.name" /></div>
        <div>description: <input asp-for="category.description" /></div>
        <button type="submit" class="btn btn-primary">save</button>
    </form>

触发的方法:

    public async task<iactionresult> onpostasync()
    {
        if (!modelstate.isvalid)
        {
            return page();
        }

        _dbcontext.categories.add(category);
        await _dbcontext.savechangesasync();

        return redirecttopage("./index");
    }

使用onpostasynconpost为处理方法名称都可以正常工作。如果您使用的是onpost,那么代码中不能使用异步调用。

但是,如果您同时实现两种onpostasynconpost等处理方法,您会遇到这样的问题:

自定义处理方法名称

除了默认的处理方法名称,我们还可以指定自定义名称。

在 .cshtml 文件中的实现以下代码:

    <form method="post">
        <div>description: <input asp-for="category.description" /></div>
        <input type="submit" value="save first" asp-page-handler="first" class="btn btn-primary btn-xs" />
    </form>

这会创建一个包含description字段的简单表单:

在razor页面中,将表单处理方法添加到匹配的 .cshtml.cs 文件代码文件,方法命名为:onpostfirst 或 onpostfirstasync ,具体取决于要在其中运行的代码类型。假设我们需要在数据库中插入category并保存这些更改,使用entity framework的异步方法:

    public async task<iactionresult> onpostfirstasync()
    {

        category.name = "first";
        _dbcontext.categories.add(category);
        await _dbcontext.savechangesasync();

        return redirecttopage("./categories/index");
    }

请注意名称 onpost first async 。

同一页面多个post处理方法

让我们扩展刚才这一段代码,添加post方法另一种形式:

下面是 .cshtml 的代码:

    <form method="post">
        <div>description: <input asp-for="category.description" /></div>
        <input type="submit" value="save first" asp-page-handler="first" class="btn btn-primary btn-xs" />
    </form>
    <form method="post">
        <div>description: <input asp-for="category.description" /></div>
        <input type="submit" value="save second" asp-page-handler="second" class="btn btn-primary btn-xs" />
    </form>

这两个表单将分别匹配代码中这两种方法:

    public async task<iactionresult> onpostfirstasync()
    {
        return await insertcatepory("first");
    }

    public async task<iactionresult> onpostsecondasync()
    {
        return await insertcatepory("second");
    }

    private async task<iactionresult> insertcatepory(string name)
    {

        category.name = name;
        _dbcontext.categories.add(category);
        await _dbcontext.savechangesasync();

        return redirecttopage("./categories/index");
    }

关键的代码是使用 asp-page-handler tag helper,指定表单的处理方法的名称。

我们也可以在一个表单通过两个提交按钮实现同样的事情:

    <form method="post">
        <div>description: <input asp-for="category.description" /></div>
        <input type="submit" value="save first" asp-page-handler="first" class="btn btn-primary btn-xs" />
        <input type="submit" value="save second" asp-page-handler="second" class="btn btn-primary btn-xs" />
    </form>

处理方法参数

将参数传递给处理方法有两种方法:

  • 表单输入
  • 表单元素借助 asp-route tag helper

通过表单输入传递参数

对于表单输入作为输入参数,名称必须是同步的。html input元素的名称必须与处理方法参数的名称相匹配:

    <form method="post">
        <input type="text" name="query"/>
        <button type="submit" asp-page-handler="search">search</button>
    </form>
    public async task onpostsearchasync(string query)
    {
        categories = await _dbcontext
            .categories
            .asnotracing()
            .where(c => !string.isnullorempty(c.description) && c.description.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>
    </div>

第一个是以前看到的search处理方法,它发送“core”作为查询参数。

第二个是针对delete处理方法,并发送id1,这表示它会删除第一条数据。

    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) {
            _dbcontext.categories.remove(category);
            await _dbcontext.savechangesasync();
        }

        return redirecttopage();
    }

到此这篇关于asp.net core中razor页面的handlers处理方法详解的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com