[英]Razor Pages, form page handler not working with GET method
I have a small ASP.NET Core Razor Pages project.我有一个小的 ASP.NET Core Razor Pages 项目。 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):在我的模型中,我有 4 个页面处理程序(其中 2 个被添加用于调试目的):
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();
}
When the form specifies method="post"
with asp-page-handler="search"
, the form calls the correct handler ( OnPostSearchAsync(String search)
).当表单使用asp-page-handler="search"
指定method="post"
时,表单会调用正确的处理程序 ( OnPostSearchAsync(String search)
)。 However, when the form specifies method="get"
with asp-page-handler="search"
, the form calls the wrong handler ( OnGetAsync()
).但是,当表单使用asp-page-handler="search"
指定method="get"
时,表单调用了错误的处理程序 ( OnGetAsync()
)。 Is this intended?这是故意的吗? If so how can i call a custom handler while using the GET
method?如果是这样,我如何在使用GET
方法时调用自定义处理程序? Maybe using a custom handler isn't necessary but i think i should be able to if i choose to.也许不需要使用自定义处理程序,但我认为如果我选择的话,我应该可以。
Here is the relevant code in .cshtml
file:这是.cshtml
文件中的相关代码:
<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>
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.本质上, asp-page-handler
设置了一个包含?handler=search
的操作 URL,然后它会被浏览器丢弃以获取 GET 请求。
In terms of workarounds, I see two:在解决方法方面,我看到两个:
Option 1 - Customise the routing选项 1 - 自定义路由
Taken straight from the docs, you can modify your page directive slightly in the .cshtml in order to customise the routing:直接从文档中获取,您可以在 .cshtml 中稍微修改页面指令以自定义路由:
@page "{handler?}"
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 eg /PageName?handler=handlerName
to /PageName/Handler
.这意味着您的调用将从/PageName?handler=handlerName
为/PageName/Handler
。 The ?
?
in the {handler?}
expression from the code-snippet simply states that a handler name is optional and will therefore default to eg OnGetAsync
.在代码片段中的{handler?}
表达式中,仅声明处理程序名称是可选的,因此将默认为例如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.这个选项有效是因为浏览器不再有一个查询字符串值可以丢弃,但是处理程序名称在路由本身中被捕获。
Option 2 - Use a hidden input选项 2 - 使用隐藏输入
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.当使用 GET 向定义的操作 URL 提交表单时,浏览器从表单中的控件构建查询字符串。 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>
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.在这里,我删除了asp-page-handler
并添加了一个隐藏输入,该输入最终会将handler
的查询字符串值设置为search
,这将构建一个与您的示例中的OnGetSearchAsync
匹配的查询字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.