剃刀页面,表单页面处理程序无法与GET方法一起使用 [英] Razor Pages, form page handler not working with GET method

查看:68
本文介绍了剃刀页面,表单页面处理程序无法与GET方法一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的ASP.NET Core Razor Pages项目.我正在制作一个具有基本搜索功能的简单列表显示页面.在我的模型中,我有4个页面处理程序(其中2个是为了调试目的而添加的):

I have a small ASP.NET Core Razor Pages project. I'm making a simple list display page with a basic search functionality. In my model, I have 4 page handlers (2 of them are added for debug purposes):

public async Task OnGetAsync()
{
    Posting = await _context.Postings
        .Include(p => p.ItemDetails).Include(p => p.Owner).ToListAsync();
}

public async Task OnPostAsync()
{
    Posting = await _context.Postings
        .Include(p => p.ItemDetails).Include(p => p.Owner).ToListAsync();
}

public async Task<PageResult> OnGetSearchAsync(String search)
{
    if (String.IsNullOrEmpty(search))
    {
        search = search.Trim();
        Posting = await _context.Postings.Where(p => p.ItemDetails.ItemName.Contains(search)).ToListAsync();
    }
    return Page();
}

public async Task<PageResult> OnPostSearchAsync(String search)
{
    if (!String.IsNullOrEmpty(search))
    {
        search = search.Trim();
        Posting = await _context.Postings
            .Where(p => p.ItemDetails.ItemName.Contains(search)).ToListAsync();
    }
    return Page();
}

当表单使用asp-page-handler="search"指定method="post"时,表单将调用正确的处理程序(OnPostSearchAsync(String search)).但是,当表单使用asp-page-handler="search"指定method="get"时,表单将调用错误的处理程序(OnGetAsync()).这是故意的吗?如果是这样,我如何在使用GET方法时调用自定义处理程序?也许没有必要使用自定义处理程序,但我想我应该可以.

When the form specifies method="post" with asp-page-handler="search", the form calls the correct handler (OnPostSearchAsync(String search)). However, when the form specifies method="get" with asp-page-handler="search", the form calls the wrong handler (OnGetAsync()). Is this intended? If so how can i call a custom handler while using the GET method? Maybe using a custom handler isn't necessary but i think i should be able to if i choose to.

这是.cshtml文件中的相关代码:

Here is the relevant code in .cshtml file:

<div id="posting_search_bar_container">
    <form method="get" asp-page-handler="search">
        <input type="text" name="search" />
        <input type="submit" value="Ara" />
    </form>
</div>
<div id="posting_list_container">
    @if (Model.Posting != null)
    {
        @foreach (var posting in Model.Posting)
        {
            <partial name="./Partials/_Posting" model="new Pages.Postings.Partials.PostingModel(posting);" />
        }
    }
</div>

推荐答案

为什么而言,此答案应该说明这里发生了什么.本质上,asp-page-handler设置了一个包含?handler=search的操作URL,然后该URL被浏览器丢弃以获取GET请求.

In terms of why this happens, this answer should explain what's going on here. Essentially, asp-page-handler sets up an action URL that includes ?handler=search, which then gets trashed by the browser for GET requests.

关于变通办法,我看到两个:

In terms of workarounds, I see two:

选项1 -直接从文档中了解一下,您可以在.cshtml中略微修改page指令,以自定义路由:

Taken straight from the docs, you can modify your page directive slightly in the .cshtml in order to customise the routing:

@page "{handler?}"

此选项指出,对于给定的页面,请使用一个额外的段来指定处理程序名称,而不是将其设置为查询字符串参数.这意味着您的通话将从/PageName?handler=handlerName/PageName/Handler.代码段中{handler?}表达式中的?仅声明处理程序名称是可选的,因此默认为例如. OnGetAsync.

This option states that for the given page, use an extra segment for specifying the handler name, rather than setting it as a query-string parameter. That means your calls will change from e.g. /PageName?handler=handlerName to /PageName/Handler. The ? in the {handler?} expression from the code-snippet simply states that a handler name is optional and will therefore default to e.g. OnGetAsync.

此选项之所以有效,是因为不再有查询字符串值可让浏览器进行垃圾回收,但处理程序名称​​ 仍被捕获在路由本身内.

This option works because there is no longer a query-string value for the browser to trash, but yet the handler name is captured within the route itself.

选项2 -使用隐藏的输入

使用GET将表单提交到已定义的操作URL时,浏览器会从表单中的控件中构建查询字符串.这提供了向表单添加新的隐藏输入字段的选项:

When submitting a form using GET to a defined action URL, the browser builds up a query-string from the controls that live within the form. This gives the option for adding a new hidden input field to the form:

<form method="get">
    <input type="hidden" name="handler" value="search" />
    <input type="text" name="search" />
    <input type="submit" value="Ara" />
</form>

在这里,我删除了asp-page-handler并添加了一个隐藏的输入,该输入最终会将handler的查询字符串值设置为search,从而建立了一个与在您的示例中.

Here, I've removed the asp-page-handler and added a hidden input that will end up setting the query-string value of handler to search, which builds up a query-string that will match for OnGetSearchAsync in your example.

这篇关于剃刀页面,表单页面处理程序无法与GET方法一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆